Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
[marto]: demonstrably yes, since you claim to want a safe space, your definition for which seems to be a place where you can name call, make things up or otherwise post without being challenged
[usemodperl]: no that's not it at all, meant safe for perl people, to not be hounded by weirdos obsessed with things like... whitespace :-)
[choroba]: I'm definitely not obsessed with whitespace, I rate myself among Perl people, and I haven't noticed any hounding
[marto]: "no it's not that"...."weirdos "...
[marto]: List EXE_FILES installed by CPAN so a couple of people suggest that your code looks obfuscated. I'd have to ageree, from the perspective of those who can't follow all of that one liner, it doesn't read well
Veltro is a weirdo, obsessed with whitespace
[marto]: to use the word "obsession" when so few people have said so little about it is grasping at staws
marto wishes tye was around, he's so much better at this sort of thing
usemodperl likes tye!
usemodperl tye  too

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-24 15:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.