Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: capturing STDERR within a script

by SysApe9000 (Acolyte)
on Mar 26, 2003 at 16:47 UTC ( [id://245991]=note: print w/replies, xml ) Need Help??


in reply to capturing STDERR within a script

close STDERR; pipe(IN,STDERR); warn "test"; close STDERR; print "X:".(<IN>);'

X:test at /tmp/ps line 3.

Make sure you close STDERR before reading from it, it hung when I did (<IN>) without first closing it. You can always re-open it before going on.

I expected that if you did something like system("find /xfkjkj") that the error message from find would also end up in IN, but it didn't. It is possible to do this, but I think you'd have to close STDIN or STDOUT first in addition to STDERR.

This has nothing to do with perl and everything to do with UNIX. When you close STDERR you are closing file descriptor 2. Then next time you open a file it will use file descriptor 2. So, when we call pipe() the input side of the pipe gets descriptor 2 and the output gets another descriptor. Now, perl writes it's warnings to STDERR, not necessarily fd 2, so everything happens as expected. But a sub-process (as created by system()) writes to fd 2 (which doesn't help us here.)

But perhaps your not on UNIX and pipe won't even work? I'll have to assume you are as I know little about other platforms.

The mini-example I posted should work and doesn't involve creating a file, but in reality you could just use a temp file and accomplish the same thing. (A temp file should work on any platform as well.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://245991]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found