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.)
|