There are many ways to read the last line of a file in perl, but as usual, not all are equal.

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use Tie::File; use File::ReadBackwards; for our $file ( qw[ data/500k.dat data/1000k.dat data/2MB.dat ]) { print "\nComparing $file"; cmpthese( -3, { 'Tie::File' => q[ my( @lines, $last ); tie @lines, 'Tie::File', $file or die $!; $last = $lines[ -1 ]; untie @lines; # print "TF:$last"; ], 'File::ReadBackwards' => q[ my $last; tie *FILE, 'File::ReadBackwards', $file or die $!; $last = <FILE>; untie *FILE; # print "RB:$last"; ], rawio => q[ my( $last, $buffer ); open FILE, '< :raw', $file or die $!; sysseek FILE, -1000, 2; sysread FILE, $buffer, 1000; $last = substr $buffer, 1+rindex( $buffer, "\n", length($b +uffer)-2 ); close FILE; # print "raw:$last"; ], readfwd => q[ my $last; open FILE, '<', $file or die $!; $last = <FILE> until eof FILE; close FILE; # print "RF:$last"; ], }); } __END__ P:\test>354830 Comparing data/500k.dat Rate Tie::File readfwd File::ReadBackwards + rawio Tie::File 5.12/s -- -94% -99% + -100% readfwd 90.0/s 1657% -- -88% + -99% File::ReadBackwards 775/s 15042% 762% -- + -95% rawio 14765/s 288372% 16314% 1805% + -- Comparing data/1000k.dat Rate Tie::File readfwd File::ReadBackwards + rawio Tie::File 2.51/s -- -95% -100% + -100% readfwd 45.9/s 1730% -- -95% + -100% File::ReadBackwards 854/s 33931% 1759% -- + -94% rawio 15214/s 605971% 33011% 1681% + -- Comparing data/2MB.dat Rate Tie::File readfwd File::ReadBackwards + rawio Tie::File 1.25/s -- -95% -100% + -100% readfwd 22.9/s 1736% -- -98% + -100% File::ReadBackwards 1040/s 83151% 4434% -- + -93% rawio 15087/s 1207959% 65694% 1351% + --

Note: The figures are for 3 files (25 character lines), on one OS (Win32XP) and perl 5.8.3. YMMV.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Reading from the end of a file. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.