· Offer Kaye > Perl > Reverse Text Order

Explanation of 'Reverse Text Order' Perl oneliner

Here is the code again: perl -lne'print scalar reverse$_'
"reverse$_" - since reverse is in the scalar context provided by "scalar", it will return a string consisting of the text of the current input line ($_) in the opposite order.
"print" - will print each line as it is returned from "reverse".
"-ln" - the command line switch "-n" takes care of iterating over the input lines, while "-l" makes sure to strip the "\n" before the reverse and to add it before the print.

Usage Example

Given an input file called "example" with the contents:
a1
b2
c3
d4

Then running the following from the command line:
perl -lne'print scalar reverse$_' example
will print to STDOUT the following four lines:
1a
2b
3c
4d

Offer Kaye / To contact me, email me at: offer.kaye no spam at gmail dot com / Last Modified: Thu Jul 15 23:04:10 2004

Valid XHTML 1.0! Valid CSS!