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?
The outputs returned from the tests (on my system) were:# 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 ); }
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).
In reply to Re: Best Way to Redirect STDERR to a Scalar
by shenme
in thread Best Way to Redirect STDERR to a Scalar
by Mr. Muskrat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |