-2

我有一个让我发疯的 perl 脚本问题。我编写了一个使用 File::Find 模块的脚本,该模块应该从给定的参数目录开始递归地遍历,并对找到的每个 *.txt 文件执行一个函数。我简化了这个问题,只显示主要部分。

为了让它运行并重现我的问题,我们必须创建一个包含两个文件的目录: fist.txt每个文件second.txt只有两行:

cat fist.txt
AAA
BBB

cat second.txt
AAA
BBB

#!/usr/bin/perl
use File::Find;

$ARGS_NUM = $#ARGV + 1;
if ($ARGS_NUM != 1) {
   print "Add start directory as an argument!\n";
   exit(-1);
}

my $DEST_DIR =$ARGV[0];    
find(\&splitter, $DEST_DIR);

sub splitter {
 if (-f $_ && /\.txt$/) {
    $DOC_FILE_NAME = $_;
    print "processing: $DOC_FILE_NAME\n";
    open $DOC_FILE, "<"."$DOC_FILE_NAME" or die "Could not open $DOC_FiLE\n";

    print "Entering first WHILE, DOC_FILE = $DOC_FILE\n";
    $AAA_FOUND = 0;
    $BBB_FOUND = 0;
    while(<$DOC_FILE>) {
      print "first_while\n";
      if (m/^AAA$/i) {
        print "FOUND: AAA in $DOC_FILE\n";
        $AAA_FOUND = 1;
        next;
      }

      if (m/^BBB$/i) {
        print "FOUND: BBB in $DOC_FILE\n";
        $BBB_FOUND = 1;
        next;
      }
    }
    #################### SECOND WHILE WCHICH FAILS.... #################
    $/="";
    seek $DOC_FILE,0,0;
    $QQQ_FOUND = 0;
    print "Entering second WHILE, DOC_FILE = $DOC_FILE\n";
    while(<$DOC_FILE>) {
      print "second_while\n";
      s/\n//g; s/$/\n/; s/^\s*//;
      if ($QQQ_FOUND == 1) {
        $question_text = $_;
        print "question_text = $question_text\n";
        last;
      }

      if (m/^QQQ.*$/i) {
        $QQQ_FOUND=1;
      }
    }
    $/ = undef;
    print "AAA = $AAA_FOUND\n";
    print "BBB = $BBB_FOUND\n";
    print "QQQ = $QQQ_FOUND\n";
    close $DOC_FILE;
  }
}

这是输出:

processing: first.txt
Entering first WHILE, DOC_FILE = GLOB(0x13087e0)
first_while
FOUND: AAA in GLOB(0x13087e0)
first_while
FOUND: BBB in GLOB(0x13087e0)
Entering second WHILE, DOC_FILE = GLOB(0x13087e0)
second_while
AAA = 1
BBB = 1
QQQ = 0
processing: second.txt
Entering first WHILE, DOC_FILE = GLOB(0x13087e0)
first_while
Entering second WHILE, DOC_FILE = GLOB(0x13087e0)
second_while
AAA = 0
BBB = 0
QQQ = 0

编辑:如您所见,第二个循环错过了搜索值 AAA 和 BBB。

4

2 回答 2

4

当然可以,因为您设置$/undef启用 slurp 模式(意思是“在一次调用中读取整个文件<>)。默认为$/not undefbut "\n"

您应该简单地使用local $/;而不是尝试手动重置它。

于 2012-11-29T17:28:25.553 回答
0

很多人觉得File::Find很烦。它根本无法正常工作。它打破了良好的编程习惯。

我发现使用它的最佳方法是在您想要的子例程之外设置一个列表变量,然后使用它来保存符合您的条件的文件。然后,您可以返回常规程序进行实际工作:

my @file_list;
find ( &wanted, $DEST_DIR);

sub wanted {
    next unless -f and  /\.txt$/;
    push @file_list, $File::Find::name;
}

# Now use @file_list to do what you need:
for my $file (@file_list) {
   yadda, yadda, yadda
}

由于想要的功能要短得多,您可以在函数中组合想要find功能:

find (
    sub {
          next unless -f and /\.txt$/;
          push @file_list, $File::Find::name;
       }, 
    $DEST_DIR
);
于 2012-11-29T17:32:24.707 回答