in reply to Reading the contents of a file from the bottom up

There's an API for that... File::ReadBackwards. I've never used it myself, but it appears to do what the documentation states.

Using a file called test.txt located in the same directory as the script itself which looks like this:

f e d c b a

...and this example script:

use warnings; use strict; use File::ReadBackwards; my $file = 'test.txt'; my $fh = File::ReadBackwards->new($file) or die "can't read the damned '$file' file!: $!"; until ($fh->eof) { print $fh->readline; }

...I get this output:

a b c d e f

Replies are listed 'Best First'.
Re^2: Reading the contents of a file from the bottom up
by perlancar (Hermit) on Nov 26, 2019 at 01:05 UTC

    Actually, no need for an API, sort of. There's PerlIO::reverse which lets you just add a layer when opening a file, then use your good ol' Perl API:

    open my $fh, "<:reverse", "/etc/passwd"; print for <$fh>;

    Like File::ReadBackwards, but unlike the tac command, PerlIO::reverse only does line-by-line reversing though.