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

it seems that when invoking unix commands in perl, the output is displayed. how is it possible to turn off the output?

Replies are listed 'Best First'.
Re: unix commands output
by Athanasius (Archbishop) on Nov 04, 2014 at 17:05 UTC

    Another useful option is the Capture::Tiny module, which allows you to capture the output for (possible) later use:

    use Capture::Tiny ':all'; my $command = ...; my @args = ...; my ($stdout, $stderr, $exit) = capture { system($command, @args); };

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: unix commands output
by toolic (Bishop) on Nov 04, 2014 at 16:01 UTC
    If you are using system, you could redirect the output to /dev/null:
    system 'ls > /dev/null';

    If using qx, just discard what is returned:

    qx(ls);
Re: unix commands output
by kennethk (Abbot) on Nov 04, 2014 at 16:40 UTC
    There are a number of ways to invoke external applications from Perl, and the general rule is that unless the method you use handles a particular I/O stream (STDIN, STDOUT, STDERR), the child connects to the parent's channel. So, when you use system, the child will use all three if it's looking for something.

    The ways to avoid this come down to either redirecting undesirable output to /dev/null, e.g. system 'call > /dev/null 2> /dev/null' or using an approach that captures the stream, like qx/backticks, IPC::Open3 or pipe/fork/exec. Note that backticks only handle STDOUT (as with IPC::Open2), so many people will use an explicit redirection of STDERR with a capture of STDOUT.

    For all the gritty details, see perlipc.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.