4

是否有(最好的)方法来检查是否$uri以单引号传递?

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

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

添加了此示例,以使我的问题更清楚。

#!/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; 
4

2 回答 2

12

你不能;bash 在将字符串传递给 Perl 解释器之前解析引号。

于 2011-06-06T09:13:52.420 回答
4

为了扩展 Blagovest 的答案......

perl program http://example.com/foo?bar=23&thing=42由 shell 解释为:

  1. 执行perl并将参数传递给programhttp://example.com/foo?bar=23
  2. 让它在后台运行(就是这个&意思)
  3. 解释thing=42为将环境变量设置thing42

您应该已经看到类似-bash: thing: command not found但在这种情况下 bash 被解释thing=42为有效指令的错误。

shell 处理引用,而 Perl 对此一无所知。Perl 不能发出错误消息,它只是在 shell 处理后看到参数。它甚至从未见过&. 这只是你必须学会​​忍受的那些 Unix 东西之一。shell 是一个完整的编程环境,无论好坏。

还有其他一些 shell 可以让事情变得很简单,所以你可以避免这个问题,但实际上你最好学习一个真正的 shell 的怪癖和功能。

于 2011-06-06T20:49:52.973 回答