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.

9 Comments

A friend showed me the following line, that I find most convenient as a sysadmin. It's always in my .bashrc ever since:

function sshdel () {
perl -i -n -e "print unless (\$. == $1)" ~/.ssh/known_hosts;
}

Also, I've picked these up, don't remember exactly where:

function google () {
    u=`perl -MURI::Escape -wle 'print "http://google.com/search?q=".
    uri_escape(join " ",  @ARGV)' $@`
    w3m -no-mouse -F $u
}

function scpan () {
u=`perl -MURI::Escape -wle 'print "http://search.cpan.org/search?query=".
uri_escape(join " ", @ARGV) ."&mode=module&n=100"' $@`

w3m -no-mouse -F $u
}


I wrote a post about my one-liner file.

Of those the only ones that I use regularly are:


# Set an xterm title
perl -e 'print "\c[];@ARGV\a"' Some title here

# Find the version number of a module
perl -le 'eval "require $ARGV[0]" and print $ARGV[0]->VERSION' Some::Module

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:


use List::Util qw(reduce);
sub list_product {
    reduce {
       [ map {
           my $i = $_; # Save outer $_
           map [ @$_, $i ], @$a
       } @$b ]
   } [[]], @_
}

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 you


(
    [1, 'a'],
    [1, 'b'],
    [2, 'a'],
    [2, 'b'],
    [3, 'a'],
    [3, 'b'],
)

The 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:

perl -MLingua::EN::Fathom -le
    "print Lingua::EN::Fathom->new->analyse_file(shift)->report;"

Kinda long for a one-liner, but I use it often, so it deserved an entry in my .bash/functions file.

# expand tiny-url
function exturl {
    perl -MLWP::UserAgent -le 'print LWP::UserAgent
                                      ->new(requests_redirectable => [])
                                      ->get(shift)
                                      ->header("Location")' "$1"
}

# 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"};'

Leave a comment

About brian d foy

user-pic I'm the author of Mastering Perl, and the co-author of Learning Perl (6th Edition), Intermediate Perl, Programming Perl (4th Edition) and Effective Perl Programming (2nd Edition).