- 論壇徽章:
- 0
|
記得之前論壇有人發(fā)帖尋求從文件末尾向前反著讀取文件的方法,這個模塊對讀取日志等大文件應該有用。讀取過程中還可以隨時切換讀取方向。
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();
復制代碼 |
|