I am currently modifying a Perl script that executes several shell commands, and, for some of them, capture the output for further processing. I want to factor out the code that executes the external commands in subroutines. I wrote the following subroutines:
sub execute_command {
my $cmd = shift;
Log("executing command $cmd ...");
system($cmd);
my $app = ( $? == -1 ) ? $? : $? >> 8;
if ($app != 0) {
Log("error executing command $cmd");
return $FAILURE;
}
Log("done");
return $SUCCESS;
}
sub execute_command_and_get_output {
my $cmd = shift;
Log("executing command $cmd ...");
unless (open( CMD, "$cmd|" )) {
Log("error executing command $cmd");
return undef;
}
my @cmd = <CMD>;
close(CMD);
Log("done");
return @cmd;
}
Questions:
-
execute_commandshould execute the passed command and return$SUCCESSif everything is OK, or$FAILUREin case of errors. Am I testing$?in the right way? -
execute_command_and_get_outputshould execute the passed command and return the output as an array (containing the output lines); if the execution of the command fails, it should returnundef. Is it correct to useunless (open( CMD, "$cmd|" )) { ... }to test for error conditions in the command execution?
Besides answers to my two questions, any suggestion for improvement is appreciated.
1) If you don’t care what error has happened, it is enough to check return value of
system:2) Instead of
return undef, it is usually better to justreturn. It works better when called in list context (it is still false). If the return is large, you might want to return reference to avoid copying.