Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Redirecting STDERR to a variable

by Anonymous Monk
on Sep 11, 2003 at 23:41 UTC ( [id://290866]=perlquestion: print w/replies, xml ) Need Help??

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

I have a perl script that calls a system command which might print some lines out to STDERR. When I run the program now, it just prints the text in the DOS window. However, I want to be able to process the STDERR text in my perl script. Is there a way to capture STDERR from a particular program execution in a perl variable?

Replies are listed 'Best First'.
Re: Redirecting STDERR to a variable
by The Mad Hatter (Priest) on Sep 12, 2003 at 00:00 UTC
    In 5.8.0, you can open file handles to "in memory" files. To open (in read-write mode) STDERR of the current script into a scalar of the same script, do this:
    my $stderr; close STDERR; open STDERR, '+<', \$stderr or die $!;
      Interesting, I might have to play with that, might solve a tiny problem I've got at work. Does it work the same for STDOUT as well?


      "Ex libris un peut de tout"
Re: Redirecting STDERR to a variable
by mildside (Friar) on Sep 12, 2003 at 01:04 UTC
Re: Redirecting STDERR to a variable
by Roger (Parson) on Sep 12, 2003 at 01:09 UTC
    The answers by people earlier only works if the perl script is trying to redirect its own output, not capturing stderr from another process.

    To capture the STDERR from another program under DOS, try the open pipe:

    use IO::File; my $f = new IO::File; $f->open("program 2>&1 1>NULL |") or die "Can not open stderr from ano +ther process."; while (<$f>) { ... do stuff }
    Note that the redirection 2>&1 and 1>NULL works under DOS.
Re: Redirecting STDERR to a variable
by nimdokk (Vicar) on Sep 11, 2003 at 23:48 UTC
    If I remember correctly, there is information on this in the perldoc pages on the open(). However, this bit of code might help point you in the right direction :-)

    open (STDERR, ">>error.log") or die "Cannot redirect STDERR to error.l +og file";
    (at least I think that is correct, most of my Perl stuff is at work right now).

    Update: Actually, in order to make the most use out of this, you will need to use qx// to run your executable. If you use system() or exec(), you may not always capture all the information you need or want. Thus:

    open (STDERR, ">>error.log") or die "Cannot redirect STDERR to error.l +og file"; qx!c:\path\to\executable!; close (STDERR);

    Of course, there is probably a better way to do all this that I have not yet discovered :-)


    "Ex libris un peut de tout"
Re: Redirecting STDERR to a variable
by teabag (Pilgrim) on Sep 12, 2003 at 15:15 UTC
    I had the same problem recently while writing a perlTk script. I found out it helped me to use the perl tie command.

    tie(*STDERR, 'Tk::Text', $text); tie(*STDOUT, 'Tk::Text', $text); $SIG{'__WARN__'} = sub { print STDERR @_ };

    You could also use the module Tie-STDERR. It has an option for:
    use Tie::STDERR \$append_to_scalar;

    good luck
    Tie-bag
    Sure there's more than one way, but one just needs one anyway - Teabag

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found