[Israel.pm] integrating unix commands

Offer Kaye oferk at oren.co.il
Wed Jan 14 00:48:30 PST 2004


>
> 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.
>

$|=1; for(`command`){do_stuff} ?

> 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
> finite.

Lost me there, it looks like the first point to me. Can you elaborate?

>
> Lastly, with backticks you can't really pipe data into a command.
>
> C'est tout, unless I missed something.
>

True, good point I didn't think of :-)
Of course, I usually do something like:
   system("cmd1 | cmd2");
But you are right- using your method is more flexible.

> Pipes are good if you care about output.

My point exactly- you don't always care about output...
1-1 :-)

> 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.
>

Sometimes I want to wait for the process to finish, sometimes I don't. But
as I see it, in both cases I could still use whichever method  I want to
call external programs.

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

Good point! :-)
I personally have been burned by the lack of portability when using
redirection operators, so I can agree with this point.

> 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.

I'm pretty sure this statement is not correct (I mean about the merged
output or only one FD). Specifically you can do a crazy thing like:
   $output = `cmd 3>&1 1>&2 2>&3 3>&-`;
(taken from a lecture by Shlomo Yona-
http://cs.haifa.ac.il/~shlomo/perl/lecture6/slide16.html)
which exchanges a command's STDOUT and STDERR. The new STDERR will come out
to wherever you redirect it to in advance...
I'm pretty sure you can also define in advance and redirect output to an
arbitrary number FD's, although I admit I'm hazy on the details.

I wonder how you would do the above with pipes, though?

-----------------------
Offer Kaye




More information about the Perl mailing list