perl -e "s/(.*)\n/\n$1/ && print scalar reverse while <STDIN>"
The first s/(.*)\n/\n$1/ simply removes the newline from the end of the input line, and places it at the start. This is so when the string is reversed, the newline will take its proper position at the end of the output line. Sure I could have used chomp, then printed out a newline with the output, but this would increase the length of the program, and the advertisement says "One line....." : not good for business :-)
The second item in the program is print scalar reverse. The reverse function reverses a list when called in list context, and a scalar when called in scalar context. Because I wanted it to reverse a single item, I had to call it in scalar context, using the scalar keyword. Note that I didn't specify any arguments, so it automatically defaulted to using $_. print then spitted out the output from reverse.
The final item is while <STDIN>. This merely loops through the input from STDIN, setting $_ each time.
)-; dnal backwards ni gniees-thgis yppaH
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Learn those command line switches!
by Abigail-II (Bishop) on Aug 05, 2003 at 11:50 UTC | |
by amrangaye (Friar) on Aug 05, 2003 at 12:14 UTC | |
|
Re: Yet another sdrawkcab translator
by poqui (Deacon) on Aug 08, 2003 at 20:05 UTC |