2015-02-09 77 views
-5

我需要修改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; 
+3

在perl中查找「sub」命令。 http://perldoc.perl.org/perlsub.html – 2015-02-09 21:53:58

+0

看起來你有核心元素 - 你只需要在foreach循環中'包裝'你的第二個腳本。 – Sobrique 2015-02-09 21:54:02

+1

@Jason:你的'x937.pl'程序有一些問題。最重要的是'$ tiff_flag'在初始化爲零後永遠不會被修改,所以'if'塊永遠不會被創建,也不會創建任何TIFF文件。這意味着'$ tiff_flag'和'$ count'變量可以被刪除。也不需要第一個'binmode',因爲開放模式是'<:raw', – Borodin 2015-02-09 22:30:14

回答

0

我想我設法正確地產生這樣(在iPad上,坐在沙發上)...有可能是一些錯別字;)

用法:perl test_x397.pl <path>

test_x397.pl

#!/usr/bin/perl -w 
use strict; use warnings; 
use Encode; 

my ($path) = @ARGV; 
$path // die "No path specified"; 
(-e $path) or die "Path not found: $path"; 
(-d $path) or die "Not a directory: $path"; 

my @files = <$path/*.x937>; 

foreach my $file (@files) { 
    process($file); 
} 

sub process { 
    my ($fname) = @_; 

    my ($dir, $file) = $fname =~ m{^(.*)/(.+)$}; 

    my $tiff_flag = 0; 
    my $count = 0; 
    my $outfile = sprintf("%s/output_%s.txt", $dir, $file); 

    open (my $outfh, '>', $outfile) or die "Unable to create $outfile. $!"; 

    open (my $infh, '<:raw', $file) or die "Error opening '$file'. $!"; 


    my $buffer = undef; 

    while (read ($infh,$buffer,4)) { 
     my $rec_len = unpack("N", $buffer); 
     die "Bad record length: $rec_len" unless ($rec_len > 0); 
     read ($infh, $buffer, $rec_len); 

     if (substr($buffer, 0, 2) eq "\xF5\xF2") { 
      if ($tiff_flag) { 
       $count++; 
       my $tiff_filename = sprintf('%s/output_%s_img%04d.tiff', $dir, $file, $count); 
       open (my $tiffh, '>', $tiff_filename) or die "Can't create image file $!"; 
       binmode($tiffh) or die 'Error setting binary mode on image file'; 
       print $tiffh substr($buffer, 117); 
       close $tiffh; 
      } 
      $buffer = substr($buffer, 0, 117); 
     } 
     print $outfh decode ('cp1047', $buffer) . "\n"; 
    } 
    close $infh; 
    close $outfh; 
} 

有幾件事情需要注意:

  1. 始終使用的開放
  2. 三個參數版本使用標文件句柄可以更容易地圍繞通過它(不在這個例子中是必要的但是很好的做法)
  3. 不要修改$ _。它可能會導致更大的程序中的令人討厭的驚喜
  4. 您已經使用sprintf來構建您的tiff文件名的一部分,所以爲什麼不把它用於整個事情。
+1

你的'$ outfh'作用於'unless'區塊,並且在'print'和'close'處不存在。它適用於OP,因爲他使用了全局文件句柄。最好是堅持流行的習語'打開我的$ outfh,'>',$ outfile或死「無法創建$ outfile:$!」'或'use autodie' – Borodin 2015-02-10 00:46:20

+0

什麼是'$ tiff_flag' - 是否設置爲外部莫名其妙? – 2015-02-10 01:04:34

+0

只是注意到鮑羅廷指出了同樣的事情在OP的評論 - 這個問題仍然站在:) – 2015-02-10 01:05:51

相關問題