Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Capturing both STDERR and STDOUT without shell redirect

by DentArthurDent (Monk)
on Aug 06, 2009 at 13:10 UTC ( [id://786414]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monks!

I've tried to RTFM, and I can't find the right FM. I have code that looks something like this:
@valid_types = `getValidTypes 2>&1`;
This works great on UNIX, where the default shell knows how to deal with the redirect, but on Windows I'm having issues. What I would like to do is have perl unify the STDOUT and STDERR from that program onto one stream without using the shell redirection. I looked at the documentation for open(), which I thought would be able to do that but I didn't see what the idiom might be.

Can anyone give me a pure Perl way to do this that would work on all platforms?

Thanks!
----
My mission: To boldy split infinitives that have never been split before!

Replies are listed 'Best First'.
Re: Capturing both STDERR and STDOUT without shell redirect
by BioLion (Curate) on Aug 06, 2009 at 13:21 UTC
Re: Capturing both STDERR and STDOUT without shell redirect
by tokpela (Chaplain) on Aug 06, 2009 at 13:59 UTC

      Seconded. I've made use of it in several scripts where I want to get a hold of all the bits of output, not mashed together, as well as the return value. Looking at one for a quick sample:

      # Way earlier use IPC::Run3; # Run it my (@out, @err); run3 \@cmd, \undef, \@out, \@err or die "run3 failed: $!"; # Return val my $rval = $? >> 8;
Re: Capturing both STDERR and STDOUT without shell redirect
by ikegami (Patriarch) on Aug 06, 2009 at 18:01 UTC
    2>&1 works in the Windows shell too
Re: Capturing both STDERR and STDOUT without shell redirect
by nimdokk (Vicar) on Aug 06, 2009 at 13:58 UTC
    While there might be better ways to do it, the way I found is as follows:

    At the start of the script redirect STDOUT and STDERR to a file. Make sure that the program you call is set to print it's output to STDOUT. Then when Perl executes the program, the output is captured into the file. This works on Unix and Windows (we run on both systems). Look in the Camel book for how to redirect STDOUT and STDERR.

Re: Capturing both STDERR and STDOUT without shell redirect
by Narveson (Chaplain) on Aug 06, 2009 at 21:31 UTC

    Here's some code I've used that implements nimdokk's suggestion above:

    open my $log_writer, '>>', $log_file; *STDOUT = $log_writer; *STDERR = $log_writer;

    where of course you have to set $log_file first. I liked this approach enough to put it in the import sub of a module I named Log::Simple.

    I'm sure there are better implementations of this idea on CPAN.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found