如果我在 Unix/Linux 系统上的 Perl 脚本中执行此操作:
open(my $fh, 'cat|');
是否涉及 shell ?如果没有,那怎么办:
open(my $fh, 'cat -v|');
如果可能的话,我想避免使用 shell,我什至知道该怎么做:
open(my $fh, '-|') || exec('cat', '-v');
但简洁也很有值(value)。
最佳答案
来自 open , 下面的代码示例
The last two examples in each block show the pipe as "list form", which is not yet supported on all platforms. A good rule of thumb is that if your platform has a real fork (in other words, if your platform is Unix, including Linux and MacOS X), you can use the list form. You would want to use the list form of the pipe so you can pass literal arguments to the command without risk of the shell interpreting any shell metacharacters in them. However, this also bars you from opening pipes to commands that intentionally contain shell metacharacters, [...]
(我的重点)
在此之前的最后一个例子是
open(my $fh, "-|", "cat", "-n", $file);
顺便说一下,这几乎正是你从问题中得到的例子。
所以使用“列表形式”并且没有 shell 。这适用于 system以及(但是,唉,不适用于 qx )
关于shell - 在 Perl 中打开管道是否涉及 shell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59549063/