有没有办法检查,如果参数在单引号中传递?

时间:2011-06-06 09:10:39

标签: perl bash uri arguments expansion

如果在单引号中传递了$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; 

2 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:4)

扩展Blagovest的答案......

shell将

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

  1. 执行perl并传递参数programhttp://example.com/foo?bar=23
  2. 让它在后台运行(这就是&的含义)
  3. thing=42解释为将环境变量thing设置为42
  4. 您应该看到类似-bash: thing: command not found的错误,但在这种情况下,bash将thing=42解释为有效指令。

    shell处理引用,Perl不知道这一点。 Perl不能发出错误消息,它只是在shell处理后看到参数。它甚至从未见过&。这只是您必须学习的Unix事物之一。无论好坏,shell都是一个完整的编程环境。

    还有其他的贝壳可以贬低一些东西,所以你可以避免这个问题,但实际上你最好还是学习真正的贝壳的怪癖和力量。

相关问题