http://www.perlmonks.org?node_id=542812

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: WHy error message is printed in STDOUT?
by holli (Abbot) on Apr 12, 2006 at 12:42 UTC
    The error message isn't sent to STDOUT. It's sent to STDERR what, for the windows shell, is the same as STDOUT (With the difference that shell piping does not work as expected.)

    See Re: catching STDERR of a system(@callout).


    holli, /regexed monk/
      Perhaps I'm being stupid but it looks to me like the message is going to STDOUT because print has been used rather than warn or print STDERR. Feel free to tell me I am stupid if I have missed something obvious.

      Cheers,

      JohnGG

        The command in the backticks, not your script, is the thing printing to stderr.

        In Unix, you could capture the stderr by redirecting stderr to also go to stdout, like this:

        $output = `$cmd 2>&1`
        That says "put the stuff going to file descriptor 2 (stderr) into file descriptor 1 (stdout)".

        I'm not sure how all that works on Windows.

Re: WHy error message is printed in STDOUT?
by Albannach (Monsignor) on Apr 12, 2006 at 14:05 UTC
    Just to combine the above correct answers into a working solution which does work under Windows (I used del for simplicity but the effect is the same):
    $cmd = 'del nonexistent.file'; chomp($res = `$cmd 2>&1`); print "\n[Command is :: ", $cmd, "]\n[RESULT:: ", $res,"]\n\n";

    Which prints:

    [Command is :: del nonexistent.file] [RESULT:: Could Not Find C:\My Documents\foo\Perl\dl\debug\nonexistent +.file]

    --
    I'd like to be able to assign to an luser

Re: WHY error message is printed in STDOUT?
by Herkum (Parson) on Apr 12, 2006 at 13:55 UTC

    The subdirectory message is being sent to STDERR by the 'md' command not Perl.

    I found this in the Perl documentation with regards to what $res is being assigned,

    The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed.