我需要修改Perl腳本x937.pl
以在特定目錄內的擴展名爲.x937
的所有文件上運行。目前,我使用一個獨立的腳本test.pl
來調用我的主腳本,併爲每個該類型的文件運行它。但是,我需要將它們合併成一個腳本。修改Perl腳本以針對給定目錄中具有指定擴展名的每個文件運行
理想情況下,我可以在腳本中指定一個目錄路徑,並遍歷該目錄中的所有*.x937
文件。
test.pl
#!/usr/bin/perl -w
use strict;
use Encode;
my @files = <*.x937>;
foreach my $file (@files) {
system('x937.pl', $file);
}
x937.pl
#!/usr/bin/perl -w
use strict;
use Encode;
use warnings;
my $tiff_flag = 0;
my $count = 0;
my $file = "output_$ARGV[0].txt";
unless (open OPUT, '>' . $file) {
die "Unable to create $file";
}
open FILE, '<:raw', $ARGV[0] or die "Error opening '$ARGV[0]' $!";
binmode(FILE) or die 'Error setting binary mode on input file';
while (read(FILE, $_, 4)) {
my $rec_len = unpack("N", $_);
die "Bad record length: $rec_len" unless ($rec_len > 0);
read(FILE, $_, $rec_len);
if (substr($_, 0, 2) eq "\xF5\xF2") {
if ($tiff_flag) {
$count++;
open(TIFF, '>', 'output_' . $ARGV[0] . '_img' . sprintf("%04d", $count) . '.tiff')
or die "Can't create image file $!";
binmode(TIFF) or die 'Error setting binary mode on image file';
print TIFF substr($_, 117);
close TIFF;
}
$_ = substr($_, 0, 117);
}
print OPUT decode('cp1047', $_) . "\n";
}
close FILE;
close OPUT;
在perl中查找「sub」命令。 http://perldoc.perl.org/perlsub.html – 2015-02-09 21:53:58
看起來你有核心元素 - 你只需要在foreach循環中'包裝'你的第二個腳本。 – Sobrique 2015-02-09 21:54:02
@Jason:你的'x937.pl'程序有一些問題。最重要的是'$ tiff_flag'在初始化爲零後永遠不會被修改,所以'if'塊永遠不會被創建,也不會創建任何TIFF文件。這意味着'$ tiff_flag'和'$ count'變量可以被刪除。也不需要第一個'binmode',因爲開放模式是'<:raw', – Borodin 2015-02-09 22:30:14