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

display tail of monster file

Thread Next
Harry Putnam
September 26, 2009 19:40
display tail of monster file
Message ID:
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:

  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;
    ## reverse @ar
    return reverse @ar;

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About