Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Best Way to Redirect STDERR to a Scalar

by shenme (Priest)
on Sep 14, 2003 at 01:17 UTC ( [id://291327]=note: print w/replies, xml ) Need Help??


in reply to Best Way to Redirect STDERR to a Scalar

Perhaps the way you were testing redirecting STDERR confused other responders.   (Then again you may have confused me - see davido's response.)   If you want to capture the STDERR output of another program that you start it gets differently complicated than just playing with the STDERR of your own program.

The various methods of TIE'ing STDERR won't work as they don't affect the STDERR filehandle (or more particularly the file descriptor ala fileno(STDERR)).   And you can play with open's using '>&STDERR' but that's still playing only with the running program.

On an *nix you could use fork and capturing output from child processes or any of the neat ways of doing the same using the open forms.   But since you mention ActiveState we should probably try for less complicated ways of doing things.   Would the code below work for you?   Namely, can you execute the program using the backtick operator?

# see qx/STRING/ in perlop, $? in perlvar, perlvar#error indicators my( @output, $status ); ($status, @output) = do_this_cmd( 'net use' ); ($status, @output) = do_this_cmd( 'net arf' ); ($status, @output) = do_this_cmd( 'netz arf' ); sub do_this_cmd { my $the_cmd = shift; my( @the_output, $error_status ); @the_output = qx($the_cmd 2>&1); $error_status = $? / 256; # Peek at arguments and returned values while debugging if(1){ printf "\nExecuted command '%s'\n", $the_cmd; printf "Process returned error status '%d'\n", $error_status if $error_status; if( @the_output ) { foreach (@the_output) { print " >>> ", $_; } } else { print "Process didn't return any output!\n"; } } ( $error_status, @the_output ); }
The outputs returned from the tests (on my system) were:

Executed command 'net use'
  >>> New connections will not be remembered.
  >>>
  >>>
  >>> Status       Local     Remote                    Network
  >>>
  >>> -------------------------------------------------------------------------------
  >>> Disconnected Z:        \\tls2ka\c$               Microsoft Windows Network
  >>> The command completed successfully.
  >>>

Executed command 'net arf'
Process returned error status '1'
  >>> The syntax of this command is:
  >>>
  >>>
  >>> NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
  >>>       HELPMSG | LOCALGROUP | NAME | PAUSE | PRINT | SEND | SESSION |
  >>>       SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]
  >>>

Executed command 'netz arf'
Process returned error status '1'
  >>> 'netz' is not recognized as an internal or external command,
  >>> operable program or batch file.
I used the net command as it prints normal output to STDOUT (first test) but error output to STDERR (second test) as does the OS (third test).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://291327]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-19 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found