[LA.pm] calling gzip from within perl

Jeremy Leader jleader at alumni.caltech.edu
Wed Jun 29 14:13:26 PDT 2005


Peter Benjamin wrote:
> At 01:31 PM 6/29/2005, Jeremy Leader wrote:
>> But it's gzip's STDOUT which is piped to FILE, it doesn't
>> affect the script's STDOUT, which is what CGI pipes back
>> to the client.
> 
> The CGI script runs as a single process id, and only has
> one STDOUT identifier in the /dev directory.
> 
> I read about this in an ORA perl book.
> Mainly as 10 years ago I tried and tried and tried,
> and never got it to work, and wondered why, so I
> went looking.
> 
> I could not quickly google an online reference.  This came close:
> 
> http://www.cs.usask.ca/resources/documentation/perl/perlfaq8.html
> 
> *STDIN, STDOUT and STDERR are shared
> *Both the main process and the backgrounded one (the ``child'' process) 
> share the same STDIN, STDOUT and STDERR filehandles. If both try to 
> access them at once, strange things can happen. You may want to close or 
> reopen these for the child.  ...snip...
> 
> --
> 
> But for CGI once you reopen the STDOUT, the parent can no longer pass
> back data to the web server to give a web page to the surfer.

Note that with '|', it's forking and execing the child, so it's
running as a different process id from the parent.  The faq entry
you reference is talking about system() or fork().

The whole point of the '|' at the end of the string passed to open()
is to tell it to do the appropriate opening, closing, and dup'ing of
filehandles in the parent and child so that the child's STDOUT is
piped to the opened handle (FILE) in the parent, and the parent's
STDOUT is left untouched.

Take a look at "perldoc -f open" (or
http://www.cs.usask.ca/resources/documentation/perl/perlfunc.html#perlfunc_open_1)
for a brief discussion of the use of '|', as well as an example of
how to do the same thing yourself (saving STDOUT, opening a new
file as STDOUT, and then reverting back to the original STDOUT),
using >& to dup filehandles.

Now if you just used fork() to run the child yourself, your
concerns about STDOUT and CGI would be absolutely correct; in
that case, you're not doing any redirection, and the parent
and child share the same STDOUT, even though they're different
processes.

-- 
Jeremy Leader
jleader at alumni.caltech.edu
leaderj at yahoo-inc.com (work)


More information about the Losangeles-pm mailing list