Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

WHy error message is printed in STDOUT?

by madtoperl (Monk)
on Apr 12, 2006 at 12:28 UTC ( #542812=perlquestion: print w/ replies, xml ) Need Help??
madtoperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi All!
I'm having a problem when running a perl script on Win32 (ActivePerl 5.8.7).

My code is as follows:

$cmd = "md \"$target\\$date\\$currentHost\\$currentDirectory\""; $res = `$cmd`; if ($debug) {print "\n[Command is :: ", $cmd, "]\n[RESULT:: ", $res,"] +\n\n"; }

The output given in response is:

A subdirectory or file C:\backups\02-02-2006\test2\C$\Documents and Settings\Administrator\Desktop already exists.
COMMAND is :: md "C:\backups\02-02-2006\test2\C$\Documents and Settings\Administrator\Desktop" RESULT ::

Can anyone explain to me why the error message is being printed to STDOUT
when the second line is getting executed instead of being stored in $res
and printed by the third line?

Comment on WHy error message is printed in STDOUT?
Download Code
Re: WHy error message is printed in STDOUT?
by holli (Monsignor) 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 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.

Re: WHy error message is printed in STDOUT?
by Albannach (Prior) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://542812]
Approved by jesuashok
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-09-02 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (28 votes), past polls