in reply to Better Way to Do it 2

Very similar, same function, but without explicit loop.
The "-n" supplies the loop necessary:
perl -ne "print scalar reverse"
Update: Oops - as folks have pointed-out(++) this reverses characters, not lines - I misread the original post.
perl -ne "print reverse <>" < file-name
Does the write thing!

Replies are listed 'Best First'.
Re: Re: Better Way to Do it 2
by shenme (Priest) on Aug 28, 2003 at 02:42 UTC
    Ahh-whoops... not the same output as above.

    The <> above is grabbing the whole file and reading all the lines into a list.   Then reverse is reversing the order of the elements, thus reversing the order of the lines.   Then the print for( ) is printing those lines one at a time.   So the file is output in reverse, last line first and first line last, but not reversing the characters within the lines.

    tedrek's code duplicates your functionality, reversing the characters of each line as it is read starting from the first line.

    Now BrowserUk's code does the same thing as desired above.   Except that it wants to crash Perl on any file too lengthy ....     (beware the smiling monk who knows the fate awaiting you)

Re: Better Way to Do it 2
by kesterkester (Hermit) on Aug 28, 2003 at 13:24 UTC
    You can even leave out the "for"!

    (The <> reads the file into a list and the reverse flips the list, which is then passed to print):

    perl -e 'print reverse <>' filename