One-line !sdrawkcab translator (for when you travel to foreign lands ;-)).
perl -e "s/(.*)\n/\n$1/ && print scalar reverse while <STDIN>"

This loops through the input from STDIN, until the user enters Ctrl-z, or ends the program with Ctrl-c.
Here is an explanation of the different parts of the program:

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
    perl -ple '$_ = reverse'

    will do the same.

      Well? TMTOWTDI, no? :-)
Re: Yet another sdrawkcab translator
by poqui (Deacon) on Aug 08, 2003 at 20:05 UTC
    Thanks for the inspiration amrangaye! :-)

    But when I kaeps sdrawkcabs I don't speak entire sentences sdrawkcab... just each word.

    So here is my version
    perl -ane 'foreach $F (@F) {print scalar reverse $F, " ";}'

    thereby picking each word out of the auto @F array and reversing it, but preserving its order in the overall line.

    the thing I dont like about it is, it eats line feeds...

    tuB rof eno ecnetnes ,selif ti skrow tsuj .enif