perl - 从后到上读取日志文件 - perl

标签 perl glob

我尝试从数组中从后到上读取日志文件,但是当日志关闭时,它会在 close($bw); 之后出现以下错误:

Not a GLOB reference at script.pl line 171.
  at /script.pl line 171.
  main::process_log("file.log") called at script.pl line 179

出现问题的脚本部分:

for my $file (@logs){
  process_log $file;
}

sub process_log {
  my $bw;

  $bw = File::ReadBackwards->new( $file ) or die "can't read $file $!" ;
  my $filename = 'status.log';
        open(FW, '>', $filename) or die $!;
        print FW "Processing file: $file\n";
        close(FW);

  $file =~ m"$logdir/(\w+)/(\w+)/(\w+\-*\w*\-*\w*)/(.*\.log)$";

  my $cmppattern = "${date}T${hour}";
  my $row;

  while( defined( $row = $bw->readline ) ) {
    if ( $row =~ m/^$cmppattern/ ) {
   
#DO SOMETHING
   
     last if ( $nlines > 0 && $row !~ m/^$cmppattern/ )
  }
  close($bw);
}

最佳答案

$bw 是一个对象,而不是文件句柄。所以你需要使用方法调用来关闭:

$bw->close();

关于perl - 从后到上读取日志文件 - perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69432649/

相关文章:

python - 在 python 中递归搜索文件的最快方法是什么?

javascript - Gulp glob 忽略文件类型而不复制空文件夹

perl - 将 perl 变量发送到命令并捕获输出

mongodb - 使用 Perl 的 Mongodb 的 SSL 连接问题

perl - 使用 Test::More 在 perl 中进行单元和集成测试 - 如何捕获退出调用?

python - 如何在 python 中更改 glob 模块的内部排序系统

Python从pandas数据框中删除停用词给出错误的输出

bash - 循环遍历具有特定扩展名的所有文件

arrays - perl qw() 函数分配行而不是单词

linux - 将小时字段增加 5(UTC 到 EST 格式)