[Israel.pm] integrating unix commands

Yuval Kogman lists at woobling.org
Tue Jan 13 09:48:17 PST 2004

On Tue, Jan 13, 2004 at 06:54:54PM +0200, Offer Kaye wrote:

> More flexible how?

With backticks you need to be able to store all of the data in memory
at once, at some point in time. That's a theoretical limit that
sometimes must be worked around - you can read from pipes in chunks.

Another aspect of flexibility is when you process - with pipes you can
do stuff with the output as it happens - so the output needn't be

Lastly, with backticks you can't really pipe data into a command.

C'est tout, unless I missed something.

> If I don't want the output from the command, I just use system:
>    system("command");
>    # optionally check return status here
> Can you even do this with pipes?

Pipes are good if you care about output. Fork && exec will have the
child work on the same FDs. This way it reads from your STDIN, and
writes to your STDOUT. When you're doing system() the parent process
is blocking on a wait call, so until the child process exits it has
absolute control of STD handles.

FYI, in a preforked environment (e.g. apache) all the processes are
doing an accept() call on the same socket, and the kernel returns the
call to one of them. The idea in a preforked environment is that at
any point in time there will always be some process blocking on an
accept call.

You can emulate this with an open3 call, dupping STDOUT, STDIN and
STDERR. You can also ask it to give the child file descriptors.

open3(0, 1, 2, 'cmd');

should be pretty comparable to


but is rather pointless.

> In the case of using backticks to get back the output you could maybe use a
> pipe, but how would it be "more flexible" then simple backticks and some
> creative redirection operators (in case STDERR is required instead of
> STDOUT, for example) usage?

That relies on the shell, which is not considered 'safe', and thus
less suited for secured environments, and less portable too.

I think the highlight is the possibility of interactivity. If you're
careful enough, your data never needs to end.

Backticks with redirects give you either merged output, or only one FD
in the result. If you want to use STDERR or STDOUT simultaneously it
gets tricker.

Don't get me wrong - backticks are wonderful in the context of perl -
they make very good use of Perl's [theoretical] lack of arbitrary
limits, they are very good for glueing things together, and they're
much more intuitive than

open FH, "-|", "cmd";
undef $\;
my $output = <FH>;

But they only go so far... =)

> Not flaming, just wondering... :-)
Grr! I'll burn your house for this. Just you wait.

 ()  Yuval Kogman <nothingmuch at woobling.org> 0xEBD27418  perl hacker &
 /\  kung foo master: /me whallops greyface with a fnord: neeyah!!!!!!!

More information about the Perl mailing list