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

Howdo

I'm looking for a module to handle backticks reliably, and gather stdout, stderr.

There seem to be a few modules out there like Proc::Short, Proc::Reliable.

Are there any others, and is there a particular module that folks would recommend (or not!) from experience?

Thanks for the help,
Rich

Replies are listed 'Best First'.
Re: Modules to handle backticks?
by Kanji (Parson) on Nov 24, 2002 at 03:15 UTC

    You might want to take at a few of the IPC:: modules like IPC::Open3 (alt.) and IPC::Run (alt.), but an alternative on platforms that support descriptor redirection is something like ...

    # adapted from the Perl Cookbook, 16.9 @all = `( cmd | perl -pe "s/^/stdout: /" ) 2>&1`; for (@all) { push @{ s/stdout: // ? \@out : \@err }, $_ }

        --k.


Re: Modules to handle backticks?
by dbp (Pilgrim) on Nov 24, 2002 at 08:04 UTC
    I'd also suggest the IPC:: modules. For example, if you want to control the input and output of another program, try something like this (Perl Cookbook, pg. 567):
    use IPC::Open2; open2(*README, *WRITEME, $program) print WRITEME "here's your input\n"; $output = <README>; close(WRITEME); close(README);
    If you want to control the input, output, and error use IPC::Open3.
Re: Modules to handle backticks?
by DamnDirtyApe (Curate) on Nov 24, 2002 at 02:59 UTC

    What exactly do you mean by "handle backticks"? Perl does backticks by itself, without any modules. They work like the qx{} operator. See perlop for more details.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Modules to handle backticks?
by Rudif (Hermit) on Nov 24, 2002 at 23:19 UTC
    >> ... and gather stdout, stderr.

    Quoting from perlop ...

    qx/STRING/ ... To capture a command's STDERR and STDOUT together: $output = `cmd 2>&1`;
    This works for me, on WinXP, as in the example below.
    To generate output going to stdout in one case and to stderr in another case, my script calls a Perforce command (p4 change -o) with a good changelist (43026) - this writes to stdout, and then with an inexistent one (143026) - this writes to stderr.
    #!/usr/bin/perl use strict; use warnings; printf STDERR "1 stdout: %s\n\n", ` p4 change -o 43026 `; printf STDERR "2 stdout: %s\n\n", ` p4 change -o 143026 `; printf STDERR "3 stdout+stderr 2>&1: %s\n\n", ` p4 change -o 143026 +2>&1 `; __END__ # typical output on my machine # 1 stdout: # A Perforce Change Specification. Change 143026 unknown. 2 stdout: 3 stdout+stderr 2>&1: Change 143026 unknown.
    HTH
    rudif
Re: Modules to handle backticks?
by Anonymous Monk on Nov 25, 2002 at 01:56 UTC
    Hi monks

    Thanks for the replies - IPC::Run looks the most promising for what I'm trying to do, but all replies are much appreciated.

    Cheers, Rich