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 (Score:1)
My most common ones are pretty simple. (Score:1)
perl -MSome::Module -e 0perl -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 (Score:2)
Or, if I need several alternatives to boring values like 'foo':
But usually I prefere the
donmartinthemeVarious one-liners from MNooning (Score:1)
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
Eval (Score:2)
Still my favorite is:
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 (Score:1)
I find I use -a rather alot for snippets like the following:
It's still useful for alternative delimiters. Pretending dpkg used tabs for delimiters, I'd add the parameter
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
The cutest of them all (Score:1)
Here’s a replacement for cat(1):
perl -please somefile.txt