If you are an
adept of the
Perl programming language, you will often find yourself typing one-liners to solve common
system administration tasks, the kind you'd solve with
sed or
grep. Perl offers in fact a special mode of invocation with the switch
-e where it gets passed a whole program on the command line, like in this case:
perl -e "print grep /^h/i, glob('*.txt');"
That prints the names of all the text files in the current directory starting with the letter h (something like
dir h*.txt, but this is just a silly example - you can use the raw power of
regular expressions to code complex matching rules).
What most people don't know is that you can actually repeat the -e switch more than once, so that you can actually build programs of more than one line using the one-liner syntax like in this case:
perl -e "print grep /^h/i, glob('*.txt');" \
-e "print 'Hello, world\n;"
This way you can be sure you'll never be out of space for your one-liners.
A different option that's often used for one-liners is the auto-loop option -n, that reads from STDIN or the passed filename and loops on each input row. This way you can build simple one liners like the following
perl -n -e "print lc;" x.txt
Where all of the lines of x.txt will be printed in lowercase to
STDOUT.
But the true Swiss-Army Chainsaw nature of Perl only shows when you add the -M (module) switch, where you can import any module available to your Perl and use it. This program, for instance, downloads and prints the Everything2 home page using the LWP::Simple web access module:
perl -MLWP::Simple -e 'print get("http://www.everything2.com";)'
That's not bad for one line of code.