in reply to Print last line in a file

If its a large file, I'd use File::ReadBackwards, otherwise just save the lines as you read them in a temp variable, then print that variable after the while loop:
my $tmp; $tmp = $_ while <FH>; print $tmp;

Replies are listed 'Best First'.
Re: Re: Print last line in a file
by Zucan (Beadle) on Aug 22, 2001 at 03:42 UTC
    If you want to get a bit crazy, you can do something like the following:

    undef $/;
    $_ = <>;
    print $1 if /([^\r\n]+\r?\n?)$/;
    

    Of course, that reads the whole file into memory, which is generally a bad idea if you don't need to do it... but hey, there is always more than one way to do it.

    Maybe somebody can provide an example of tying a variable to the file and then using the regex? Personally, Runrig's example is probably the best way to do it.

    Zucan