有没有办法检查参数是否以单引号传递?

发布于 2024-11-13 11:05:11 字数 985 浏览 4 评论 0 原文

是否有(最佳)方法来检查 $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; 

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; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七颜 2024-11-20 11:05:12

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

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

乄_柒ぐ汐 2024-11-20 11:05:12

要扩展 Blagovest 的答案...

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

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

您应该看到类似 -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:

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文