in reply to Test whether STDOUT is connected to a file

rovf:

I think that the problem you're having is that the perl executable doesn't manage I/O redirection. Normally that's handled by the shell. So the command you're giving doesn't actually try to redirect STDOUT and STDERR--instead it's just passing ">out.txt" and "2>&1" as arguments to the myProg.pl script.

Why not simply add command-line options to myProg.pl to have alternate output streams? Then you don't have to detect redirection--you just tell myProg where to send the output.

...roboticus

  • Comment on Re: Test whether STDOUT is connected to a file

Replies are listed 'Best First'.
Re^2: Test whether STDOUT is connected to a file
by rovf (Priest) on Aug 18, 2010 at 14:20 UTC
    BTW, I double-checked the perldocs. The case about no redirection performed is documented there, but only for the implementation on RISC OS. About Windows, this restriction is not mentioned. Actually, the docs don't say anything at all about the Windows implementation.

    However, I now found a reproducible case where the problem appear. What surprises me is the fact that it looks so simple:

    ... system(1,'perl.exe',$0,'SLAVE1','>out1.txt','2>&1'); ...
    Here I found that the redirection is never set up, and indeed if I do a -t STDOUT in the child process, I always get a true value. Now it puzzles me why we have in our application so many cases which work. My best guess is that Perl, in those cases, finds something which makes it create an intermediate CMD.EXE process which sets up the redirection.

    (Just in case you wonder: The reason why I call myself recursively via $0 is that my test program distinguishes by its first parameter whether it should act as a child instead as a parent).

    -- 
    Ronald Fischer <ynnor@mm.st>
Re^2: Test whether STDOUT is connected to a file
by rovf (Priest) on Aug 18, 2010 at 13:48 UTC
    I think that the problem you're having is that the perl executable doesn't manage I/O redirection.
    This can't be correct, for two reasons: If it were the case, the error should occur all the time (and not only occasionally), and @ARGV in the child process would have the "redirection parameters".

    Actually, no shell isn't involved in this particular case. Redirection is performed in this case by the calling process, by system(1,...).

    -- 
    Ronald Fischer <ynnor@mm.st>