in reply to Performing a tail(1) in Perl (reading the last N lines of a file)

I'm not sure how this benchmarks, but it is a little more succinct ;-)
use Tie::File; my $length = shift @ARGV; my $file = shift @ARGV; tie(my @file, "Tie::File", $file, autochomp => 0) or die("ack - $!"); print @file[$#file - $length + 1 .. $#file];
_________
broquaint

Update: fixed slice code as it was off by one (as noted by grinder)

  • Comment on Re: Performing a tail(1) in Perl (reading the last N lines of a file)
  • Download Code

Replies are listed 'Best First'.
Re: Performing a tail(1) in Perl (reading the last N lines of a file)
by Dominus (Parson) on Apr 26, 2002 at 03:37 UTC
    Says broquaint:
    I'm not sure how this benchmarks, but it is a little more succinct ;-)
    use Tie::File; ... print @file[$#file - $length + 1 .. $#file];
    It should benchmark OK, although not as well as a special-purpose backward-file-reader. But I agree with you about the succinctness. I mostly followed up to point out that you can do even better:
    print @file[-$length .. -1];
    Hope this helps.

    --
    Mark Dominus
    Perl Paraphernalia