Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

BOTH STOUT and STDERR to same file.

by hmb104 (Sexton)
on Sep 03, 2012 at 18:38 UTC ( #991499=perlquestion: print w/replies, xml ) Need Help??
hmb104 has asked for the wisdom of the Perl Monks concerning the following question:

Monks monks monks,

I want to send BOTH STOUT and STDERR to my output.txt file. I know it should be simple, but for some reason I'm getting the error but not the other output. what is a good way to fix get it working?

#!/usr/local/bin/perl use warnings; use strict; my $input = shift; my $output = "/home/me/output.txt"; open (MYFILE, "$input"); while (<MYFILE>) { chomp; system("/home/me/ $_ >$output 2>&1"); } close (MYFILE);

Replies are listed 'Best First'.
Re: BOTH STOUT and STDERR to same file.
by davido (Archbishop) on Sep 03, 2012 at 19:13 UTC

    I've mostly stopped bothering with trying to deal with shell redirection and the portability issues it raises. Capture::Tiny abstracts the details away and lets me get on with my work. Here's the module's decription:

    Capture::Tiny provides a simple, portable way to capture almost anything sent to STDOUT or STDERR, regardless of whether it comes from Perl, from XS code or from an external program. Optionally, output can be teed so that it is captured while being passed through to the original filehandles. Yes, it even works on Windows (usually). Stop guessing which of a dozen capturing modules to use in any particular situation and just use this one.

    The capture_merged function should give you all the ammunition you need.

    use strict; use warnings; use Capture::Tiny qw( capture_merged ); my $input = shift; my $output = '/home/me/output.txt'; my $run = '/home/me/'; open my $infh, '<', $input or die $!; open my $ofh, '>', $output or die $!; while( <$infh> ) { chomp; my( $captured, @results ) = capture_merged { system $run, $_ }; print $ofh $captured; } close $ofh or die $!; close $infh;


Re: BOTH STOUT and STDERR to same file.
by aitap (Curate) on Sep 03, 2012 at 19:02 UTC
    system("/home/me/ $_ >$output 2>&1");
    It is very unsafe (imagine somebody writing "; rm -rf /*" in the input file). Try using IPC::Run, which does all redirections itself.
    Sorry if my advice was wrong.
Re: BOTH STOUT and STDERR to same file.
by remiah (Hermit) on Sep 03, 2012 at 20:01 UTC
    system("/home/me/ $_ >$output 2>&1");
    This will overwrite output file in while loop.
    What if you append the output?
    system("/home/me/ $_ >> $output 2>&1");

      All I needed was to append instead of re-write my lines. Thanks for the help :).

Re: BOTH STOUT and STDERR to same file.
by Anonymous Monk on Sep 04, 2012 at 00:39 UTC
Re: BOTH STOUT and STDERR to same file.
by cheekuperl (Monk) on Sep 04, 2012 at 02:50 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991499]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2018-04-24 09:34 GMT
Find Nodes?
    Voting Booth?