home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
- From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
- Subject: Re: fork and STDOUT
- Message-ID: <mcook.725818562@fendahl.dev.cdx.mot.com>
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: fendahl.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1992Dec30.223911.16297@rchland.ibm.com>
- Date: Thu, 31 Dec 1992 16:16:02 GMT
- Lines: 39
-
- essbaum@rchland.vnet.ibm.com (Alexander Essbaum) writes:
-
- >i'm doing
-
- >if (!($pid = fork)) {
- > open (cmd,"$cmd|"); # $cmd is a call to another perl script
- > print <cmd>;
- > exit 0;
- >}
-
- >[...] it seems to be getting buffered such that
- >when $cmd exits, all of STDERR gets printed, then all of STDOUT.
-
- It *is* being buffered. This line:
-
- print <cmd>;
-
- is like this:
-
- @anon = <cmd>
- print @anon;
-
- All of cmd's output is read, and *then* it is printed. Instead, do this:
-
- print while <cmd>;
-
- Also, the STDERR output will not travel through the pipe. To see that, try
- this:
-
- print "FOO:", $_ while <cmd>;
-
- You'll see that the STDERR lines are not prefixed by "FOO:". To capture the
- STDERR output, too, it's often easiest to have the shell help:
-
- open (cmd,"( $cmd ) 2>&1 |");
-
- (The parens may be superfluous, if $cmd is simple.)
-
- Michael.
-