- 论坛徽章:
- 0
|
记得之前论坛有人发帖寻求从文件末尾向前反着读取文件的方法,这个模块对读取日志等大文件应该有用。读取过程中还可以随时切换读取方向。
http://search.cpan.org/~kianwin/ ... le/Bidirectional.pm- use File::Bidirectional;
- my $file = "/var/log/large_file";
- # Object interface
- # start from the last line
- my $fh = File::Bidirectional->new($file, {origin => -1})
- or die $!;
- # read backwards until point of interest
- while (my $line = $fh->readline()) {
- last if $line =~ /RECORD_START/;
- }
- # switch directions
- $fh->switch();
- # read forwards until point of interest
- while (my $line = $fh->readline()) {
- last if $line =~ /RECORD_END/;
- }
- # Tied Handle Interface
- local *F;
- tie *F, "File::Bidirectional", $file, {origin => 1}
- or die $!;
- while (my $line = <F>) { ... }
- (tied *F)->switch();
复制代码 |
|