0

我有两个目录,一个目录是文件HG111_1_001.txt,另一个目录是HG111_2_001.txt。这两个文件需要通过一组命令(sub single 和 sub double)进行处理,然后作为一对带回 sub playnice。我遇到的问题如下:

代码返回以下模棱两可的警告: Use of uninitialized value $file2 in string at C:\Users\prl\tools.pl 第 52 行。这将是最后的打印行。

启用使用 Carp 时,此错误指示: main::playnice(HG111_2_001.txt 在第 37 行调用和 main::playnice(HG111_1_001.txt 在第 21 行调用

这些是我将值传递给子时对应的行。

#!/usr/bin/perl -w

use strict;
use warnings;

my $dirname  = 'C:\Users\prl';
my $dirname2 = 'C:\Users\prl\1';

my $file1;
my $file2;

#Read Directory and find first file
opendir( D, $dirname2 ) or die "can't opendir $dirname: $!";
while ( $file2 = readdir(D) ) {

    next unless $file2 =~ m/^HG111_1_0/;

    my $path2 = "$dirname2\\$file2";
    single( $file2, $path2 );
    playnice($file2);
}

#Pass to first sub
sub single {
    my $file2 = shift;
    my $path2 = shift;
    print "$path2\n";
    print "$file2\n";
}

opendir( DIR, $dirname ) or die "can't opendir $dirname: $!";
while ( $file1 = readdir(DIR) ) {
    next unless $file1 =~ m/^HG111_2_0/;
    my $path2 = "$dirname\\$file1";

    double( $file1, $path2 );
    playnice($file1);

}

sub double {
    my $file1 = shift;
    my $path1 = shift;
    print "$path1\n";
    print "$file1\n";
}

sub playnice {
    my $file1 = shift;
    my $file2 = shift;

    print "$file1", "$file2", "\n", 'orked?';
}
4

1 回答 1

1

你只将一个参数传递给你的 playnice 函数......

看这个简单的例子:

sub playnice {
  my ($f1,$f2) = @_;
  print "$f1 $f2\n";
}

# your program: wrong, $f2 is undef
playnice('foo'); # one argument

# what it should do
playnice('foo','bar') # two argument

现在您必须编辑代码并使用两个参数调用 playnice 函数。

你也应该:

  • 考虑将所有子程序放在同一个地方(在程序的开头/结尾);
  • 清楚地告诉我们该计划的目的;
  • 清理你的代码(添加缩进和注释)
于 2013-11-29T23:44:22.897 回答