You want to process each line or paragraph of a text file in reverse.
Read all lines into an array, then process that array from the end to the start:
@lines = <FILE>; while ($line = pop @lines) { # do something with $line }
Or store an array of lines in reverse order:
@lines = reverse <FILE>; foreach $line (@lines) { # do something with $line }
The limitations of file access mentioned in this chapter's Introduction prevent you from reading a line at a time starting from the end. You must read the lines into memory, then process them in reverse order. Needless to say, this requires at least as much available memory as the size of the file.
The first technique moves through the array of lines, in reverse order. This destructively processes the array, popping an element off the end of the array each time through the loop. We could do it non-destructively with:
for ($i = $#lines; $i != -1; $i--) { $line = $lines[$i]; }
The second approach generates an array of lines that is already in reverse order. This array can then be processed non-destructively. We get the reversed lines because the assignment to @lines
forces list context on reverse
, which in turn forces it on <FILE>
. <>
in a list context returns a list of all lines in the file.
These approaches are easily extended to paragraphs just by changing $/
:
# this enclosing block keeps local $/ temporary { local $/ = ''; @paragraphs = reverse <FILE>; } foreach $paragraph (@paragraphs) { # do something }
The reverse
function in perlfunc (1) and in Chapter 3 of Programming Perl; the $/
entry in perlvar (1), and in the "Special Variables" section of Chapter 2 of Programming Perl; Recipe 4.10; Recipe 1.6