Getting stderr when using '-|'

Discussion in 'Perl Misc' started by bill, Dec 30, 2004.

  1. bill

    bill Guest

    I want to use exec($prog, @args) to execute a system command without
    worrying about shell escapes, but I also want to capture both stdout
    and stderr from this system command. If I only wanted stdout, I
    could do something like this

    die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));

    if ($pid == 0) {
    exec($prog, @argv) or die "Can't exec $prog: $!\n";
    exit 0; # superfluous
    }

    my $output = do { local $/ = undef;, <$read_child> };
    close $read_child;
    if ($?) {
    die "Child failed: $?"
    }

    In the code above, $output captures the stdout from the command
    run through exec. But how does one capture stderr as well? Not
    surprisingly adding the string '2>&1' to exec's arguments fails
    because exec just passes this string along as one more argument to
    $prog.

    Any help would be much appreciated.

    bill
    bill, Dec 30, 2004
    #1
    1. Advertising

  2. bill

    KKramsch Guest

    In <cr0539$so4$> bill <> writes:

    >I want to use exec($prog, @args) to execute a system command without
    >worrying about shell escapes, but I also want to capture both stdout
    >and stderr from this system command. If I only wanted stdout, I
    >could do something like this


    >die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));


    >if ($pid == 0) {


    open (STDERR, '>&STDOUT') or die "Can't dup STDOUT\n";

    > exec($prog, @argv) or die "Can't exec $prog: $!\n";
    > exit 0; # superfluous
    >}


    >my $output = do { local $/ = undef;, <$read_child> };
    >close $read_child;
    >if ($?) {
    > die "Child failed: $?"
    >}




    HTH,

    Karl

    --
    Sent from a spam-bucket account; I check it once in a blue moon. If
    you still want to e-mail me, cut out the extension from my address,
    and make the obvious substitutions on what's left.
    KKramsch, Dec 30, 2004
    #2
    1. Advertising

  3. bill

    Paul Lalli Guest

    "bill" <> wrote in message
    news:cr0539$so4$...
    >
    > I want to use exec($prog, @args) to execute a system command without
    > worrying about shell escapes, but I also want to capture both stdout
    > and stderr from this system command.



    Have you read the appropriate FAQ on this topic?
    perldoc -q stderr


    Paul Lalli
    Paul Lalli, Dec 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    11,470
  2. horos22
    Replies:
    15
    Views:
    1,528
  3. Damphyr
    Replies:
    2
    Views:
    191
    Damphyr
    Dec 8, 2005
  4. Replies:
    7
    Views:
    329
    Jan Svitok
    Mar 2, 2007
  5. Zbigniew Fiedorowicz

    Getting STDERR from forked child processes?

    Zbigniew Fiedorowicz, Feb 5, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    155
    Aaron Sherman
    Feb 5, 2004
Loading...

Share This Page