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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|