External programs

There are three ways to call external programs.

system() returns the exit status of the program

    my $rc = system("/bin/cp $file1 $file2"); # returns exit status values
    die "system() failed with status $rc" unless $rc == 0;

If possible, pass your arguments as a list, rather than a single string.

    my $rc = system("/bin/cp", $file1, $file2 );

This makes sure that nothing goes wrong in the shell if $file1 or $file2 have spaces or other special characters.

The output from system() is not captured.

XXX Discuss lower 8-bits of return code.

XXX Discuss running through a pipe

Backticks (``) and qx() operator return the output of the program

When you want the output, use one of these.

    my $output = `myprogram foo bar`.

You'll need to check the error code in $!.

If you're using backticks or qx(), prefer IPC::Open2 or IPC::Open3 instead, since they'll give you the same argument control and allow you to capture the output as well.

IPC::Open3 is the only way to capture STDERR in Perl without using shell commands.

We want your feedback

If we can improve in any way, please let us know with this form.

Your name
Your email
Your comment