Using perl 5.8.0 on Solaris, I sometimes use system calls to manipulate a file or directory. This has worked well in the past, but this week a script failed trying to copy a file. The error message was "Illegal seek at ./script.pl line XXX", and the system call causing the error is here:
use strict;
system( "cp /analysis/fasta1.fa /analysis2/fasta1.fa" )
or die print "Can't copy fasta file: $! \n";
A friend at work said that in his experience, the return code from "system" isn't reliable when used that way. He said I should capture the actual return code from the system call and evaluate it. If it's not zero, there's an error and to print $! at that point.
I followed his recommendation and the problem went away. Here's the code I used.
use strict;
&doSystemCommand( "cp /analysis/fasta1.fa /analysis2/fasta1.fa" );
sub doSystemCommand
{
my $systemCommand = $_[0];
print LOG "$0: Executing [$systemCommand] \n";
my $returnCode = system( $systemCommand );
if ( $returnCode != 0 )
{
die "Failed executing [$systemCommand]\n";
}
}
Could you tell me more about what is happening here and why this eliminated the errors? Also, could you offer improvements in this function for handling system calls?