in reply to Reading the last line of a file in a Pure Way.


This does tail -1 in a pure (evil) way:     perl -pe '${}}=${_}}{${_}=${}}' file

--
John.

Replies are listed 'Best First'.
Re2: Reading the last line of a file in a Pure Way.
by blakem (Monsignor) on Aug 09, 2002 at 06:49 UTC
      Please enlighten me

      how does this work and what is the }{

      I realise -p feeds all lines to the script, sets the ORS to each one but how is this suppressing printing and what is the magic }{

      Thanks if you spare the time

        -p wraps a loop around your code. You can see what it looks like:
        $ perl -MO=Deparse -pe '$\=$_}{' LINE: while (defined($_ = <ARGV>)) { $\ = $_; } { (); } continue { die "-p destination: $!\n" unless print $_; }
        In effect, the }{ breaks off the continue part of the while loop that usually prints every line, so it only runs once at the end of file. Since $_ will have been set to undef at end of file, copying it to $\ preserves it so that print will show it. ($\ is implicitly added to each print by perl.)