4

Is there a (best) way to check, if $uri was passed in single quotes?

#!/usr/local/bin/perl
use warnings;
use 5.012;

my $uri = shift;
# uri_check
# ...

Added this example, to make my problem more clear.

#!/usr/local/bin/perl
use warnings;
use 5.012;
use URI;
use URI::Escape;
use WWW::YouTube::Info::Simple;
use Term::Clui;

my $uri = shift;
# uri check here

$uri = URI->new( $uri );
my %params = $uri->query_form;
die "Malformed URL or missing parameter" if $params{v} eq '';
my $video_id = uri_escape( $params{v} );

my $yt = WWW::YouTube::Info::Simple->new( $video_id );
my $info = $yt->get_info();

my $res = $yt->get_resolution();
my @resolution;
for my $fmt ( sort { $a <=> $b }  keys %$res ) {
    push @resolution,  sprintf "%d : %s", $fmt, $res->{$fmt};

}

# with an uri-argument which is not passed in single quotes 
# the script doesn't get this far

my $fmt = choose( 'Resolution', @resolution );
$fmt = ( split /\s:\s/, $fmt )[0];
say $fmt; 
sid_com
  • 24,137
  • 26
  • 96
  • 187
  • 2
    possible duplicate of [How to parse quoted as well as unquoted arguments in Perl?](http://stackoverflow.com/questions/6201584/how-to-parse-quoted-as-well-as-unquoted-arguments-in-perl) – Quentin Jun 06 '11 at 09:21
  • When I call your program thus: `perl so6249816.pl http://www.youtube.com/watch?v=TkqgnVW2_aA` (note, no shell quoting whatsoever) it really does go until line 27 and beyond. Your comment in the source says it won't. Explain this contradiction. – daxim Jun 06 '11 at 11:46
  • When I copy a `video URL` from a YouTube-Video I get a format like this `http://www.youtube.com/watch?v=BzSTSkhBCOc&feature=player_detailpage` which doesn't work on my tries without quotes. – sid_com Jun 06 '11 at 16:32

2 Answers2

12

You can't; bash parses the quotes before the string is passed to the Perl interpreter.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • 2
    @Grrrr: Well, I said bash because it is in the tags of the question. – Blagovest Buyukliev Jun 06 '11 at 11:20
  • 4
    @sid_com it seems clear that this is an "XY problem" (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) so a better question is "Why do you think that knowing how the shell args are quoted will help you solve whatever your real (unstated) problem is?" IOW, why do you think you need to know how it was quoted? – tadmc Jun 06 '11 at 12:18
  • An error-message concerning the quotes would be nice, because I don't know what I remember, when I use the script the next time. – sid_com Jun 06 '11 at 15:44
4

To expand on Blagovest's answer...

perl program http://example.com/foo?bar=23&thing=42 is interpreted by the shell as:

  1. Execute perl and pass it the arguments program and http://example.com/foo?bar=23
  2. Make it run in the background (that's what & means)
  3. Interpret thing=42 as setting the environment variable thing to be 42

You should have seen an error like -bash: thing: command not found but in this case bash interpreted thing=42 as a valid instruction.

The shell handles the quoting and Perl has no knowledge of that. Perl can't issue an error message, it just sees arguments after shell processing. It never even sees the &. This is just one of those Unix things you'll have to learn to live with. The shell is a complete programming environment, for better or worse.

There are other shells which dumb things down quite a bit so you can avoid this issue, but really you're better off learning the quirks and powers of a real shell.

Schwern
  • 153,029
  • 25
  • 195
  • 336