Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Thursday December 17, 2009
01:19 AM

What's your coolest Perl one-liner?

[ #40028 ]

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. :)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I was very proud of this recent substitution that implements the Euclidean algorithm: http://perlmonks.org/?node_id=804838 [perlmonks.org] It follows in the fine tradition of the regex primality test by using repeated capturing groups to perform division with remainder.
  • 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.

  • ~$ 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

  • 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.0285
  • 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.

  • 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'

  • Here’s a replacement for cat(1):

    perl -please somefile.txt