有没有办法检查参数是否以单引号传递?
是否有(最佳)方法来检查 $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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能; bash 在将字符串传递给 Perl 解释器之前解析引号。
You can't; bash parses the quotes before the string is passed to the Perl interpreter.
要扩展 Blagovest 的答案...
perl 程序 http://example.com/ foo?bar=23&thing=42
由 shell 解释为:perl
并向其传递参数program
和http://example.com/foo?bar=23
&
的意思)thing=42
解释为设置环境变量thing
为42
您应该看到类似
-bash: thing: command not find
的错误,但在本例中 bash 解释为 < code>thing=42 作为有效指令。shell 处理引用,而 Perl 对此一无所知。 Perl 不能发出错误消息,它只是在 shell 处理后看到参数。它甚至从未看到
&
。这只是您必须学会忍受的 Unix 事物之一。无论好坏,shell 都是一个完整的编程环境。还有其他 shell 可以使事情变得简单一些,这样您就可以避免这个问题,但实际上您最好学习真正 shell 的怪癖和功能。
To expand on Blagovest's answer...
perl program http://example.com/foo?bar=23&thing=42
is interpreted by the shell as:perl
and pass it the argumentsprogram
andhttp://example.com/foo?bar=23
&
means)thing=42
as setting the environment variablething
to be42
You should have seen an error like
-bash: thing: command not found
but in this case bash interpretedthing=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.