Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm looking for a solution for the following problem:

On Win32 (e.g. ActiveState PERL) My PERL-Scripts get invoked by a MAKEfile (NMAKE).

The whole NMAKE call may / or may not have STDERR redirected to STDOUT nmake.exe makefile target 2>&1

I now want to detect if STDERR is redirected to STDOUT, to avoid duplicate writing of Warnings/Errors.

For Unix I have found something like:
sub dev_ino_of_handle { my $handle = shift; join (" ", (stat $handle)); } sub send_maybe_both { my $text = shift; print STDOUT $text; unless (dev_ino_of_handle(\*STDOUT) eq dev_ino_of_handle(\*STDERR)) +{ print STDERR $text; } }
But this does not work under Windows. Any hints?

Replies are listed 'Best First'.
Re: How to detect I/O redirection on Win32
by BrowserUk (Patriarch) on Sep 10, 2007 at 16:55 UTC

    You should not be printing errors and warnings to both channels in the first place.

    By doing so, you are preventing the user from ignoring them, and that is presumptive.

    Only write errors and warnings to STDERR and allow the user to decide whether they wish to redirect them or ignore them, and then your question becomes moot or mute or irrelevant.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi,

      the PERL scripts are part of a software release process (CM) and I definitely DON'T WANT the users to ignore errors or warnings.

      The log file is parsed later and transformed into HTML where warnings and errors get colored....

      If the whole subprocess is redirected I need to print out errors/warnings ONCE and if it is not I have to print both to STDERR and STDOUT and eventually to a LOGFILE handle...

      And as long as the "master" process is NOT controlled by a PERL script, but some BATchfile I cannot control redirection /logging to the log file.

Re: How to detect I/O redirection on Win32
by Roy Johnson (Monsignor) on Sep 10, 2007 at 16:21 UTC
    fileno Update: it's what the documentation seemed to be saying, but isn't.

    Caution: Contents may have been coded under pressure.
      Nope
      >perl -le"print fileno(STDOUT); print fileno(STDERR);" 2>&1 1 2
Re: How to detect I/O redirection on Win32
by Anonymous Monk on Sep 10, 2007 at 16:08 UTC
    FYI, now that I could log in: I started this node... :-)