in reply to Re: Read the last line in a text file
in thread Read the last line in a text file

Or Tie::File, or
print [<FILE>]->[-1];
[]s, HTH, Massa (κς,πμ,πλ)

Replies are listed 'Best First'.
Re^3: Read the last line in a text file
by moritz (Cardinal) on Aug 22, 2008 at 12:08 UTC
    print [<FILE>]->[-1];

    If you mention such things you should also add that it reads the entire file into memory. That might be a bit overkill...

    Another thing that came to my mind, but is restricted to unixy systems:

    my $line = `tail -n1 $file`;
      While it does read the entire file, and while it keeps an index of every line in memory, it doesn't keep the entire file in memory.
      I stand corrected, you are obviously right. This should work without the memory overhead:
      print do { my $l; $l = $_ while <FILE>; $l }
      []s, HTH, Massa (κς,πμ,πλ)
Re^3: Read the last line in a text file
by blazar (Canon) on Aug 22, 2008 at 22:40 UTC

    In addition to the more important observation that moritz made, I can never understand why so often do people seem to feel the need to reference only to dereference like that; i.e. if we assume we do want to slurp the file all at once, then

    print +(<$file>)[-1]; # would do!
    --
    If you can't understand the incipit, then please check the IPB Campaign.
      [X]->[Y] is more symmetric than +(X)[Y]... ;-)
      []s, HTH, Massa (κς,πμ,πλ)