in reply to Re: Re: Reading from STDOUT after Redirecting it.
in thread Reading from STDOUT after Redirecting it.
Child processes inherit all file descriptors, but a file handle is an internal Perl thing and isn't inherited in the way you'd expect.
When a new process starts up, standard in is on file descriptor 0, standard out on file descriptor 1, and standard error on file descriptor 2. To redirect one of these, a Perl script has to assign a new file to one of these file descriptors. But that's not what happens by just reassigning STDOUT:
#!/usr/bin/perl open(STDOUT,"> /tmp/t26.out");
$ strace -e open perl /tmp/t26
...
open("/tmp/t26.out", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
Fortunately, it's not that hard to work around; open always returns the lowest numbered file descriptor, so closing STDOUT right before re-opening it will do what you expect:
#!/usr/bin/perl close(STDOUT); open(STDOUT,"> /tmp/t27.out");
$ strace -e open perl /tmp/t27
...
open("/tmp/t27.out", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 1
|
|---|