open and STDERR

G

grocery_stocker

"Found in /usr/lib/perl5/5.8.3/pod/perlfaq8.pod
How can I capture STDERR from an external command?

There are three basic ways of running external commands:

system $cmd; # using system()
$output = ‘$cmd‘; # using backticks (‘‘)
open (PIPE, "cmd │"); # using open()

With system(), both STDOUT and STDERR will go the same
place as the script’s STDOUT and STDERR, unless the sys­
tem() command redirects them. Backticks and open() read
only the STDOUT of your command."

If open() reads only to STDOUT, Then how when I do something like the
following:

#!/usr/bin/perl -w

open(STDERR, "whore.txt") or die "Cant: $! \n";

I get:
miss_xtc@linux:~/perl> ./op.pl
Cant: No such file or directory

Wouldn't this be writing to STDERR in this case?
 
A

anno4000

grocery_stocker said:
"Found in /usr/lib/perl5/5.8.3/pod/perlfaq8.pod
How can I capture STDERR from an external command?

There are three basic ways of running external commands:

system $cmd; # using system()
$output = ‘$cmd‘; # using backticks (‘‘)
open (PIPE, "cmd │"); # using open()

With system(), both STDOUT and STDERR will go the same
place as the script’s STDOUT and STDERR, unless the sys­
tem() command redirects them. Backticks and open() read
only the STDOUT of your command."

If open() reads only to STDOUT, Then how when I do something like the
following:

#!/usr/bin/perl -w

open(STDERR, "whore.txt") or die "Cant: $! \n";

That tries to open "whore.txt" for reading, which fails unless the
file already exists.
I get:
miss_xtc@linux:~/perl> ./op.pl
Cant: No such file or directory

Wouldn't this be writing to STDERR in this case?

Why should it? You have to open the file for writing:

open STDERR, '>', 'whore.txt' or die "Can't create 'whore.txt': $!";

Anno
 
G

grocery_stocker

That tries to open "whore.txt" for reading, which fails unless the
file already exists.


Why should it? You have to open the file for writing:

open STDERR, '>', 'whore.txt' or die "Can't create 'whore.txt': $!";

Anno

If I understand the faq, something like
$output = `$cmd`;

Reads only the STDOUT. Where is STDERR going in this case?
 
E

Eric Amick

If I understand the faq, something like
$output = `$cmd`;

Reads only the STDOUT. Where is STDERR going in this case?

Nowhere, i.e., the bit bucket. If you want it to go somewhere, redirect
stderr in the command according to the command shell's conventions. For
example,

$output = `$cmd 2>/some/file/or/other`

on *nix sends stderr to /some/file/or/other. (This is all in perlop.)
 
J

Joe Smith

grocery_stocker said:
If I understand the faq, something like
$output = `$cmd`;

Reads only the STDOUT. Where is STDERR going in this case?

STDERR from $cmd goes to the same place that your perl program's
STDERR is going. Use

$output = `$cmd 2>&1`;

if you want STDERR to go to the same place as STDOUT. In that
case, $output will have both, and your program may have to
search for and/or remove the error messages from it.
-Joe
 
G

grocery_stocker

Joe said:
STDERR from $cmd goes to the same place that your perl program's
STDERR is going. Use

$output = `$cmd 2>&1`;

if you want STDERR to go to the same place as STDOUT. In that
case, $output will have both, and your program may have to
search for and/or remove the error messages from it.
-Joe

Further down the perl faq, they give the following solution
To capture a program's STDOUT, but discard its STDERR:

use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
while( <PH> ) { }
waitpid($pid, 0);

they don't use
$output = `$cmd 2>&1`;

I'm assuming the solution is perl faq done for portability purposes. I
don't have a Windows box,
otherwise I would I would see if something like

$output = `$cmd 2>&1`;

would have worked in Windows XP
 
J

Joe Smith

grocery_stocker said:
don't have a Windows box,
otherwise I would I would see if something like

$output = `$cmd 2>&1`;

would have worked in Windows XP

It does.

C:\>perl -le "$_=`net file 2>&1`;s/\s*$//;print'>>>',$_,'<<<'"
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top