Perl命令行问题

发布于 2024-10-31 12:46:17 字数 620 浏览 0 评论 0原文

我正在编写一个 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 技术交流群。

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

发布评论

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

评论(3

残疾 2024-11-07 12:46:17

当 Perl 看到

my $c = `cmd \c "`.$ARGV[0].`" /A "page=`.$ARGV[1].`=OpenActions" "`.$ARGV[2].``;

它时,它会变成这样:

my $c = qx{cmd \c "}.$ARGV[0].qx{" /A "page=}.
        $ARGV[1].qx{=OpenActions" "}.$ARGV[2].qx{};

每个 qx{...} 部分,在遇到时由命令 shell 执行,其中大多数可能是语法错误。您的完整命令永远不会运行。

您可能想要的是:

my $c = qx{cmd \\c "$ARGV[0]" /A "page=$ARGV[1]=OpenActions" "$ARGV[2]"};

构造字符串,然后将其传递给 shell。

When Perl sees

my $c = `cmd \c "`.$ARGV[0].`" /A "page=`.$ARGV[1].`=OpenActions" "`.$ARGV[2].``;

It turns it into something like this:

my $c = qx{cmd \c "}.$ARGV[0].qx{" /A "page=}.
        $ARGV[1].qx{=OpenActions" "}.$ARGV[2].qx{};

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:

my $c = qx{cmd \\c "$ARGV[0]" /A "page=$ARGV[1]=OpenActions" "$ARGV[2]"};

Which constructs the string, and then passes it to the shell.

倾`听者〃 2024-11-07 12:46:17

我认为您对反引号的工作原理有点困惑。像这样的事情:

my $c = `/where_is/pancake_house`

将运行 /where_is/pancake_house 命令并将其标准输出上打印的任何内容放入 $c 中。反引号也像双引号字符串一样进行插值。您还必须转义反斜杠。

因此,您不需要在命令中使用多组反引号,也不需要像这样将内容粘贴在一起。像这样的事情:

my $c = `cmd \\c "$ARGV[0]" /A "page=$ARGV[1]=OpenActions" "$ARGV[2]`;

应该没问题。当然,如果 ARGV 值包含空格、引号或其他有趣的东西,您仍然会遇到问题。您可能想使用 IPC::Open2IPC::Open3< /code>以提高安全性。

I think you're a little confused about how backticks work. Something like this:

my $c = `/where_is/pancake_house`

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:

my $c = `cmd \\c "$ARGV[0]" /A "page=$ARGV[1]=OpenActions" "$ARGV[2]`;

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 use IPC::Open2 or IPC::Open3 for more safety.

梦里兽 2024-11-07 12:46:17

反引号并不像您想象的那样工作。

$var = "foo";
`cmd \c `.$foo

这不会执行命令 cmd \c foo。它执行命令 cmd \c 并获取输出,将 $foo 的值连接到该输出。您需要构造整个命令,然后将其提供给反引号运算符。

Backtick doesn't work like you seem to think.

$var = "foo";
`cmd \c `.$foo

This does not execute the command cmd \c foo. It executes the command cmd \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.

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