What are the best Perl one-liners you've written?
Josh McAdams and I are about to finish off Effective Perl Programming, which is already on Amazon for pre-order. We just have to finish off the last item, a list of really cool Perl one-liners. We have the stuff that we use ourselves, but we know there is a lot more out there.
What's in your login files? Have you written a really cool Perl one-liner?
We're especially interested in one-liners that do interesting Windows things. Do you have something that interacts with Office (or maybe plays MineSweeper for you)?
Maybe you have something that you type in the SSH shell on your iPhone? Are you doing something with Perl and Android?
Show us what you have, give us a couple of sentences about what it does, and how you'd like your name to appear in the book.
A friend showed me the following line, that I find most convenient as a sysadmin. It's always in my .bashrc ever since:
Also, I've picked these up, don't remember exactly where:
I wrote a post about my one-liner file.
Of those the only ones that I use regularly are:
I should add that the xterm one-liner is dependent on environmental variables or processes not overriding the title.
Not technically a oneliner because it's not standalone code, but one that I like anyway: cartesian product in one tweet. With the air let back into it, it's:
The upshot of which is that it gives you the cartesian product in an arbitrary number of dimensions, e.g.
list_product [1, 2, 3], ['a', 'b']
gets youThe Perl calculator:
perl -ple '$_=eval'
The Poor-Man's Grep (for Winodws):
perl -nle "BEGIN{@ARGV=map{glob}@ARGV}/PATTERN/&&print" <filespec> ...
Replace PATTERN with the regular expression pattern to search for.
This one-liner analyzes the linguistic properties of a piece of English text. It's useful for figuring out the piece's complexity, so you can temper it for various audience types:
Kinda long for a one-liner, but I use it often, so it deserved an entry in my .bash/functions file.
# Remove the first column of a file
perl -nale 'shift@F;print"@F"'
Nothing fancy, but it's handy when you want to pull IP addresses out of log files
cat /var/log/messages|perl -e 'while (>) {/(\d+\.\d+\.\d+\.\d+)/ && print "$1\n"};'