传递其中包含点运算符的命令行参数

发布于 2024-12-11 14:35:11 字数 347 浏览 0 评论 0原文

我试图解析其中一个网站,同时我需要通过

$url="http://www.hotels.com/search/search.html?destinationName=Dallas%2C+Texas%2C+United+States&arrivalDate=10%2F27%2F11&departureDate=10%2F31%2F11&numberOfRooms=1&numberOfAdults=1";
system("perl x.pl $url"); // this is giving me error because the $url consists of dot operator.

i was trying to parse one of the website while doing so i need to pass

$url="http://www.hotels.com/search/search.html?destinationName=Dallas%2C+Texas%2C+United+States&arrivalDate=10%2F27%2F11&departureDate=10%2F31%2F11&numberOfRooms=1&numberOfAdults=1";
system("perl x.pl $url"); // this is giving me error because the $url consists of dot operator.

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

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

发布评论

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

评论(2

千笙结 2024-12-18 14:35:11

在这种情况下,来自 system() 的多参数效果更好:

system('perl', 'x.pl', $url);

当您传递 system() 包含任何 shell 元字符(包括空格)的单个字符串时,它会调用它通过 /bin/sh (至少在类 Unix 系统上)——这意味着诸如 & 字符之类的东西将由 shell 解释,除非 (正如@MikePlayle建议的那样)你引用它。

但是,当您给 system() 多个参数时,它会绕过 shell 并直接执行第一个参数中指定的命令,并将其他参数传递给它。

当您希望 shell 处理元字符时,单参数形式非常有用 - 例如,如果您想要执行 I/O 重定向(包括管道)和/或通配符扩展,可以用 Perl 完成,但需要付出更多的努力。

perldoc perlfunc 并搜索“system”以获取更多信息,或参见此处< /a>.

The multi-argument from of system() is better in this case:

system('perl', 'x.pl', $url);

When you pass system() a single string that contains any shell metacharacters (including spaces), it invokes it via /bin/sh (at least on Unix-like systems) -- which means that things like the & character are going to be interpreted by the shell unless (as @MikePlayle suggests) you quote it.

But when you give system() multiple arguments, it bypasses the shell and executes the command named in the first argument directly, passing it the other arguments.

The single-argument form is useful when you want the shell to handle metacharacters -- for example, if you want to do I/O redirection (including pipes) and/or wildcard expansion, things that can be done in Perl, but with a bit more effort.

perldoc perlfunc and search for "system" for more information, or see here.

鸩远一方 2024-12-18 14:35:11

您可能需要在 URL 两边加上引号,这样 shell 就不会尝试解释它。

尝试类似

system("perl x.pl \"$url\"");

Or 的任何 Perl 语法用于引号。我确信如果我做错了,会有人纠正我。

You probably need to put quotes round the URL so the shell doesn't try to interpret it.

Try something like

system("perl x.pl \"$url\"");

Or whatever the Perl syntax is for quotes. I'm sure someone will correct me if I've got it wrong.

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