perl system call not executing randomly

Within a loop construct of a perl script, I have written the following lines to parse text files using system tools, generating temporary text files in the process, and subsequently read the temporary output into an array for processing within the perl script:

system("awk '(NR+2)%4==0' $infile[$i+$j] | tre-agrep -ns -E $dist[$a][$b] -k $query[$a][$b] | awk 'BEGIN{FS=":";OFS=":"}{print $1,$2}' > $outfile");
open(my $FH, "<", $outfile) || die "Can't open $outfile: $!";
while(<$FH>) {
  ...
}
close($FH);

These commands are repeated twice nearly verbatim (with modification of some of the parameters, but recycling of the file handle) within a loop construct that itself is iterated numerous times. Unexpectedly and seemingly arbitrarily, the program sometimes fails to complete the system call, causing the subsequent lines, which depend on the output generated by the system call, to fail in turn, triggering abortion of the script and display of the rather unhelpful error message "No such file or directory" (with reference to the open statement). Executing the same system call directly from a console rather than within the context of the perl script shows that the command produces the expected output. I refer to this behavior as arbitrary because sometimes my script will variously complete 1 to 3 iterations before failing at the open line, and the basis for the varying success is not clear. When the script is working properly, the system call takes quite some time (around 2 minutes), whereas when it fails, the program moves to the following open line in less than a second. I would thus like to figure out why the system call is sometimes skipped.

The script is run in a bash shell session and the following are included in the script header:

#! /usr/bin/perl
use warnings;
use strict;

I fundamentally agree with @ThisSuitIsBlackNot. However, not knowing what tre-agrep is, it's difficult to translate that part into straight Perl.

That said, at the very least, why not skip the generation of the output file and just read the Unix output directly from Perl?

open my $FH, '-|', "awk '(NR+2)%4==0' $infile[$i+$j] | " .
    "tre-agrep -ns -E $dist[$a][$b] -k $query[$a][$b]" or die "$!";
while (<$FH>) {
  chomp;
  my ($field1, $field2) = split /:/, $_, 2;
}
close $FH;

At worst, the standard output from your system call would be blank, but this wouldn't impact Perl's ability to read nothing (and therefore do nothing).

Of course, it wouldn't hurt to first execute an -e (exists) call to be sure that infile[$i + $j] isn't a ghost.

链接地址: http://www.djcxy.com/p/54588.html

上一篇: Travis CI使用非安装perl模块

下一篇: perl系统调用不会随机执行