develooper Front page | perl.beginners | Postings from September 2009

display tail of monster file

Thread Next
From:
Harry Putnam
Date:
September 26, 2009 19:40
Subject:
display tail of monster file
Message ID:
87k4zlkt54.fsf@newsguy.com
Will it make any difference in loading time or cpu resource usage to
use the `tail' command like 'qx/tail -n30 $file/' as apposed to something
in straight perl like this example that prints the last 30 lines of $log:

  #!/usr/local/bin/perl 
  
  use strict;
  use warnings;
  
  my $log = shift;
  
  print rb($log);
  
  sub rb {
    use File::ReadBackwards;
    my $log = shift;
    my (@ar,$logline);
  
    my $bw = File::ReadBackwards->new( "$log" ) or
         die "can't read <$log>: $!" ;          
  
    my $cnt = 0;
    while( defined( $logline = $bw->readline ) ) {
      $cnt++ if($logline);
      if($cnt <= 30) {
        push @ar,$logline;
      }else{
        last;
      }
    }
    ## reverse @ar
    return reverse @ar;
  }
  




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About