If I do this in a Perl script on a Unix/Linux system:
open(my $fh, 'cat|');
is a shell involved? And if not, what about this:
open(my $fh, 'cat -v|');
I would like to avoid a shell, if possible, and I even know how to do it:
open(my $fh, '-|') || exec('cat', '-v');
but brevity is also valuable.
Perl's open function opens a pipe instead of a file when you append or prepend a pipe symbol to the second argument to open . This turns the rest of the arguments into a command, which will be interpreted as a process (or set of processes) that you want to pipe a stream of data either into or out of.
Pipe may be the most useful tool in your shell scripting toolbox. It is one of the most used, but also, one of the most misunderstood. As a result, it is often overused or misused. This should help you use a pipe correctly and hopefully make your shell scripts much faster and more efficient.
A pipeline is a sequence of one or more commands separated by one of the control operators ' | ' or ' |& '. The format for a pipeline is. [time [-p]] [!] command1 [ | or |& command2 ] … The output of each command in the pipeline is connected via a pipe to the input of the next command.
From open, following code examples
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, [...]
(my emphasis)
The last example previous to this is
open(my $fh, "-|", "cat", "-n", $file);
which is incidentally almost exactly your example from the question.
So go with the "list form" and there's no shell. This goes for system as well (but, alas, not for qx)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With