从后往前读取日志文件 - perl
Read log files from back to top - perl
我尝试从数组中从后到上读取日志文件,但是当日志关闭时,它在 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();
我尝试从数组中从后到上读取日志文件,但是当日志关闭时,它在 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();