将给定目录中具有指定扩展名的每个文件的 Perl 脚本修改为 运行

Modify Perl script to run for each file with specified extension in a given directory

我需要将特定目录中扩展名为 .x937 的所有文件的 Perl 脚本 x937.pl 修改为 运行。目前,我使用一个单独的脚本 test.pl 来调用我的主脚本,并为该类型的每个文件 运行 调用它。但是,我需要将两者合并到一个脚本中。

理想情况下,我可以在脚本中指定一个目录路径,并循环遍历该目录中的所有 *.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;

我想我成功地生成了这个(在 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 文件名,那么为什么不将它用于整个文件名。