What's your coolest Perl one-liner?

brian_d_foy on 2009-12-17T06:19:18

Josh and I are finishing up Effective Perl Programming, 2nd Edition, and the last part to finish off is the item on Perl one-liners.

Besides going through writing a one liner, we want to list a bunch of them too. Want to get your name in the book? Give us some one-liners that you wrote yourself and a couple of sentences about what it does. Make sure you tell us how you'd like your name to appear in the book, too. :)


GCDs in a regex

JadeNB on 2009-12-17T15:38:05

I was very proud of this recent substitution that implements the Euclidean algorithm:

http://perlmonks.org/?node_id=804838

It follows in the fine tradition of the regex primality test by using repeated capturing groups to perform division with remainder.

My most common ones are pretty simple.

elliot on 2009-12-17T16:04:58

perl -MSome::Module -e 0

perl -E 'say foreach split ":", $ENV{PATH}' # Or other pathlike thing.

ack --text -l '\bv1.2.3\b' | xargs perl -pi -e 's/\bv1\.2\.3\b/v1.2.4/g'

Otherwise, it's mostly experiments; I prefer bash to a REPL.

MetaSyntactic

domm on 2009-12-17T22:29:05

~$ perl -MAcme::MetaSyntactic=donmartin -E 'say metaname'
KLINK

Or, if I need several alternatives to boring values like 'foo':

~$ perl -MAcme::MetaSyntactic=any -E 'say metaname for 1..10'
l5nas_parchan
The_Living_Daylights
sugar
TO
Nick
Neptune
Pigpen
b romine
Kyrano
voyager

But usually I prefere the donmartin theme

Various one-liners from MNooning

mhn on 2009-12-18T21:09:54

MNooning wrote:

Here are some Windows one-liners. On Linux, use single quotes and forward slashes.


Find the INC path that Perl sees when it starts up.
>perl -e "print qq[INC = @INC\n];"
INC = C:/Perl/site/lib C:/Perl/lib .

Another way to find the INC path.
perl -wle "print for @INC"
C:/Perl/site/lib
C:/Perl/lib

Find out if a module is installed. If it is, no error messages will show.
>perl -e "use Tk"

Obtain the version of an installed module.
>perl -MTk -e "print \"$Tk::VERSION\n\"
804.028501

Here is the same command on Cygwin
>$ perl -MTk -e 'print $Tk::VERSION'
804.028


This is what you see if the module is not there:
>perl -e "use TkBad"
Can't locate TkBad.pm in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .) at -e line 1. BEGIN failed--compilation aborted at -e line 1.


This is also what you see if the module is not there:
>perl -MTkBad -e "print \"$TkBad::VERSION\n\"
Can't locate TkBad.pm in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .). BEGIN failed--compilation aborted.

Here is an example of getting the version of an installed module in a deep path. Again, the syntax is different on Linux.
C:\oldProjects\proj_bu_2003\routines>perl -MCatalyst::Authentication::Store::LDAP -e "print \"$Catalyst::Authentication: :Store::LDAP::VERSION\n\"
0.1005

This is a bit of a different one-liner subject, but a quick way to scroll a module's package file is
>perldoc -m Tk

Eval

Juerd on 2009-12-21T01:53:26

Still my favorite is:

perl -ple'$_=eval'

It's a pure REPL. It evaluates every line from stdin and outputs whatever they evaluate to. It has many uses, but most of the time it's "just" a very powerful calculator.

The awk features are rather nice

jjore on 2009-12-21T02:02:17

I find I use -a rather alot for snippets like the following:

dpkg -l | perl -lane 'print $F[1] if $F[1] =~ /ruby/'

It's still useful for alternative delimiters. Pretending dpkg used tabs for delimiters, I'd add the parameter

-F$'\t'

It also find it's really helpful to got a proper handle on quoting whether in my shell or in perl. Often in a one-liner I'm using qq{} in perl to avoid messing with my shell's own use of ' or ". Occasionally I use bash's $'\t' syntax which lets me say things like

perl -F$'\t'

. I often say the ugly but effective:

perl -le 'print "Cant'\''t get no satisfaction"'

I use the very, very short one-liner

perl -dea

which just opens up a fresh debugger. I like that "dea" is supposedly the Latin word for goddess. I gather other people like to have other cute words like -debugger. It's all rather arbitrary but works as long as the word starts with the letters 'de'.

The cutest of them all

Aristotle on 2009-12-22T20:16:31

Here’s a replacement for cat(1):

perl -please somefile.txt