home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / perl / 7634 < prev    next >
Encoding:
Internet Message Format  |  1992-12-30  |  1.9 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!usenet.coe.montana.edu!news.u.washington.edu!stein.u.washington.edu!wiml
  2. From: wiml@stein.u.washington.edu (William Lewis)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: fork and STDOUT
  5. Date: 31 Dec 1992 04:39:07 GMT
  6. Organization: University of Washington
  7. Lines: 31
  8. Message-ID: <1htthbINNqg9@shelley.u.washington.edu>
  9. References: <1992Dec30.223911.16297@rchland.ibm.com>
  10. NNTP-Posting-Host: stein.u.washington.edu
  11.  
  12. essbaum@rchland.vnet.ibm.com (Alexander Essbaum) writes:
  13. >if (!($pid = fork)) {
  14. >    open (cmd,"$cmd|");   # $cmd is a call to another perl script
  15. >    print <cmd>;
  16. >    exit 0;
  17. >}
  18.  
  19. >the stuff returned in <cmd> (which comes from print "..."; and `...`; lines
  20. >in $cmd) doesn't print properly.  it seems to be getting buffered such that
  21. >when $cmd exits, all of STDERR gets printed, then all of STDOUT. 
  22.  
  23.   Generally, stder doesn't get redirected along with stdout. (That's
  24. its purpose for being separate.) I'd guess that the error output is
  25. going straight to your tty when $cmd runs, but stdout is getting trapped
  26. in the pipe. Then you print the pipe's contents on the next line. So
  27. all the error output will precede the normal output.
  28.  
  29.  
  30. >open (something,">&STDOUT");
  31.  
  32. >can someone explain exactly what this does (what is "duping"?) or maybe
  33. >point me where to look (i checked the FAQ)?
  34.  
  35.   The dup() call (in unix) takes a file descriptor and returns another
  36. file decriptor which references the same file (or tty or socket or
  37. whatever). The dup2() call does the same thing, except you can specify
  38. which file descriptor you want it to return. So if you specify stdout,
  39. then stdout will start behaving just like whatever fd it was you duplicated
  40. into it. (This is how you redirect output into a file, for example.)
  41. I'm not sure if I'm being entirely clear here, but the Unix man pages for
  42. dup() and dup2() are the place to look.
  43.