Perl 中系统命令输出的文件句柄

发布于 2024-09-09 04:41:39 字数 37 浏览 3 评论 0原文

我在 Perl 中执行的系统命令的输出是否有文件句柄/句柄?

Is there a filehandle/handle for the output of a system command I execute in Perl?

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

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

发布评论

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

评论(2

蘑菇王子 2024-09-16 04:41:39

下面是在脚本和其他命令之间建立管道的示例,使用 的 3 参数形式打开

open(my $incoming_pipe, '-|', 'ls -l')             or die $!;
open(my $outgoing_pipe, '|-', "grep -v '[02468]'") or die $!;

my @listing = <$incoming_pipe>;          # Lines from output of ls -l
print $outgoing_pipe "$_\n" for 1 .. 50; # 1 3 5 7 9 11 ...

Here's an example of establishing pipes between your script and other commands, using the 3-argument form of open:

open(my $incoming_pipe, '-|', 'ls -l')             or die $!;
open(my $outgoing_pipe, '|-', "grep -v '[02468]'") or die $!;

my @listing = <$incoming_pipe>;          # Lines from output of ls -l
print $outgoing_pipe "$_\n" for 1 .. 50; # 1 3 5 7 9 11 ...
一口甜 2024-09-16 04:41:39

是的,您可以使用这样的管道:

open(my $pipe, "ls|") or die "Cannot open process: $!";
while (<$pipe>) {
    print;
}

请参阅 open 的文档 了解更多信息,perlipc 了解完整信息管道操作的描述。

Yes, you can use a pipe like this:

open(my $pipe, "ls|") or die "Cannot open process: $!";
while (<$pipe>) {
    print;
}

See the documentation for open for more information, and perlipc for a complete description of pipe operation.

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