There are three ways to call external programs.
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
$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
When you want the output, use one of these.
my $output = `myprogram foo bar`.
You'll need to check the error code in
IPC::Open3 is the only way to capture
STDERR in Perl without using shell commands.
Submit a PR to github.com/petdance/perl101
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.