Perl命令行问题
我正在编写一个 Perl 程序,它将采用一些命令行参数(它们实际上由另一个程序提供)并打开 pdf 到特定页面。我基于此处(查看第 5 页)。我已经直接从命令行测试了该命令,它完全符合我的要求。现在我尝试从 Perl 中执行此操作,但似乎不起作用。我得到的错误是:
The process tried to write to a nonexistent pipe
这是代码...有人可以告诉我我做错了什么吗?
#!C:/perl/bin/perl
use strict;
use warnings;
use diagnostics;
my $c = `cmd \c "`.$ARGV[0].`" /A "page=`.$ARGV[1].`=OpenActions" "`.$ARGV[2].``;
print $c;
system "Pause";
我得到的只是 cmd 中的一个空格。一旦我按下 Ctrl+C,它就会返回到提示符,如果我按下 Enter 键,就会出现上述错误。
I'm writing a Perl program that will take a few command-line arguments (they'll actually be supplied by another program) and open a pdf to a specific page. I based it off of here (Look at page 5). I've already tested the command straight from the command line, and it does exactly what I want it to do. Now I'm trying to do it from Perl, and it doesn't appear to be working. The error I get is:
The process tried to write to a nonexistent pipe
Here's the code... can someone tell me what I'm doing wrong?
#!C:/perl/bin/perl
use strict;
use warnings;
use diagnostics;
my $c = `cmd \c "`.$ARGV[0].`" /A "page=`.$ARGV[1].`=OpenActions" "`.$ARGV[2].``;
print $c;
system "Pause";
All I get after this is a blank space in cmd. Once I hit Ctrl+C, it returns to a prompt, and if I hit enter there, it gives me the above error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当 Perl 看到
它时,它会变成这样:
每个
qx{...}
部分,在遇到时由命令 shell 执行,其中大多数可能是语法错误。您的完整命令永远不会运行。您可能想要的是:
构造字符串,然后将其传递给 shell。
When Perl sees
It turns it into something like this:
Each of those
qx{...}
portions, are executed by the command shell as they are encountered, most of them are probably syntax errors. Your full command is never run.What you probably wanted was:
Which constructs the string, and then passes it to the shell.
我认为您对反引号的工作原理有点困惑。像这样的事情:
将运行
/where_is/pancake_house
命令并将其标准输出上打印的任何内容放入$c
中。反引号也像双引号字符串一样进行插值。您还必须转义反斜杠。因此,您不需要在命令中使用多组反引号,也不需要像这样将内容粘贴在一起。像这样的事情:
应该没问题。当然,如果
ARGV
值包含空格、引号或其他有趣的东西,您仍然会遇到问题。您可能想使用IPC::Open2
或IPC::Open3< /code>
以提高安全性。
I think you're a little confused about how backticks work. Something like this:
Will run the
/where_is/pancake_house
command and put whatever it prints on its standard output into$c
. Backticks also interpolate like double quote strings. You'll also have to escape backslashes.So, you don't want multiple sets of backticks in command and you don't need to paste things together like that. Something like this:
should be okay. Of course you'll still have issues if the
ARGV
values have spaces or quotes or other funny things. You'd probably want to useIPC::Open2
orIPC::Open3
for more safety.反引号并不像您想象的那样工作。
这不会执行命令
cmd \c foo
。它执行命令cmd \c
并获取输出,将$foo
的值连接到该输出。您需要构造整个命令,然后将其提供给反引号运算符。Backtick doesn't work like you seem to think.
This does not execute the command
cmd \c foo
. It executes the commandcmd \c
and takes the output, concatenates the value of$foo
to that output. You need to construct the entire command, and only then feed it to the backtick operator.