Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!sdd.hp.com!swrinde!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: How can I capture STDERR from a command executed externally Message-ID: <1991May23.055849.6100@jpl-devvax.jpl.nasa.gov> Date: 23 May 91 05:58:49 GMT References: <4352@inews.intel.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: usa Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 55 In article <4352@inews.intel.com> rpaul@sedona.intel.com writes: : : I want to be able to capture STDERR from an external command and do some : substitution and reprint it back out. Following is an experiment I was doing : to get this to work. The ls command used is the GNU ls, which with the -h : option, prints its options to stderr: : : #!/stor/gnu/bin/perl : : open(STDERR, ">&STDOUT"); Fine. : open(STDOUT); What's this supposed to do? : select(STDERR); $| = 1; # make unbuffered You mean, make STDERR command buffered. Note that this has no effect on the buffering of subprocesses. : open(TMP, "ls -h >&STDERR"); This is just plain wrong. You're trying to open a file called "ls -h >&STDERR", which I suspect doesn't exist in your current directory. You have to use a vertical bar to indicate a piped command, and even if you do that, the string STDERR will certainly not be recognized by /bin/sh, which only knows about numeric file descriptors. : while() Why are you reading from STDOUT? I'd think you'd want to read from TMP. : { : s/\+/\%/g; : print $_; : } : : close(TMP); : close(STDERR); : close(STDOUT); : : The stderr is still going to the tty and not the STDERR filehandle. Is there : a way to capture the STDERR output of the TMP filehandle. You're making this all much too hard. Just say open(TMP, "ls -h 2>&1 |"); while () { tr/+/%/; print; } Larry