home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / perl / 7641 < prev    next >
Encoding:
Text File  |  1992-12-31  |  1.3 KB  |  52 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: fork and STDOUT
  5. Message-ID: <mcook.725818562@fendahl.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  8. Organization: Motorola Codex, Canton, Massachusetts
  9. References: <1992Dec30.223911.16297@rchland.ibm.com>
  10. Date: Thu, 31 Dec 1992 16:16:02 GMT
  11. Lines: 39
  12.  
  13. essbaum@rchland.vnet.ibm.com (Alexander Essbaum) writes:
  14.  
  15. >i'm doing
  16.  
  17. >if (!($pid = fork)) {
  18. >    open (cmd,"$cmd|");   # $cmd is a call to another perl script
  19. >    print <cmd>;
  20. >    exit 0;
  21. >}
  22.  
  23. >[...]  it seems to be getting buffered such that
  24. >when $cmd exits, all of STDERR gets printed, then all of STDOUT.
  25.  
  26. It *is* being buffered.  This line:
  27.  
  28.     print <cmd>;
  29.  
  30. is like this:
  31.  
  32.     @anon = <cmd>
  33.     print @anon;
  34.  
  35. All of cmd's output is read, and *then* it is printed.  Instead, do this:
  36.  
  37.     print while <cmd>;
  38.  
  39. Also, the STDERR output will not travel through the pipe.  To see that, try
  40. this:
  41.  
  42.     print "FOO:", $_ while <cmd>;
  43.  
  44. You'll see that the STDERR lines are not prefixed by "FOO:".  To capture the
  45. STDERR output, too, it's often easiest to have the shell help:
  46.  
  47.     open (cmd,"( $cmd ) 2>&1 |");
  48.  
  49. (The parens may be superfluous, if $cmd is simple.)
  50.  
  51. Michael.
  52.