

10 tips for writing perl one-liners - nelhage
http://blog.ksplice.com/2010/05/top-10-perl-one-liner-tricks/

======
jrockway
Perl is dead. One-liners are unmaintainable. Everyone should write their one-
off command invocations with test driven development, separation of interface
and implementation, javadoc, and aspect-oriented programming.

~~~
zavulon
I guess it's "in style" now to make fun of "Perl is unmaintainable"
statements. Sure, it's cool and a lot of fun, until you're the one that has to
maintain it and make changes to it.

~~~
jrockway
I maintain a lot of Perl. It's a joy.

Now this legacy C++ and Java... hoo boy...

------
pkrumins
Also see my article on "Perl One-Liners Explained":

[http://www.catonmat.net/blog/perl-one-liners-explained-
part-...](http://www.catonmat.net/blog/perl-one-liners-explained-part-one/)

~~~
sigzero
I have! I liked it very much. I was using a Perl dos2unix one liner this
morning in fact. I love Perl.

------
devinj
I think this article has convinced me that I should learn Perl. Looks bloody
useful for shell scripts, indeed ("a better awk" is the phrasing I've heard).

~~~
sreque
No one will argue with you that Perl isn't better than awk, if that's the bar
you've set for yourself for picking a new language to learn.

~~~
zrail
I use perl every day and awk almost every day. Awk is useful for commands
where it forms a small part of an overall pipeline and needs to be somewhat
terse, and yet fast. Gawk is actually fairly fast, mawk[1] even more so.

[1]: <http://invisible-island.net/mawk/mawk.html>

------
d0mine
`END{ }` could be replace by eskimo operator `}{`:

    
    
      perl -F, -lane '$t += $F[1]; }{ print $t'
    

[http://www.perlfoundation.org/perl5/index.cgi?eskimo_operato...](http://www.perlfoundation.org/perl5/index.cgi?eskimo_operator)

`..` is called flip-flop in the context describe in the post.

Goatse operator `=()=`:

    
    
      my $count_vowels =()= /[aeiou]/g;
    

Here's another usage for `\K`:

    
    
      echo '/a/b/c/def/gh' | perl -lnE'$d{$`}++while/\w\K\b/g }{$,=" ";say keys%d'
    

Output:

    
    
      /a/b/c/def/gh /a/b/c /a/b/c/def /a/b /a
    

[http://stackoverflow.com/questions/2892126/file-fix-it-
codeg...](http://stackoverflow.com/questions/2892126/file-fix-it-codegolf-
gcj-2010-1b-a/2894170#2894170)

Other features: [http://stackoverflow.com/questions/161872/hidden-features-
of...](http://stackoverflow.com/questions/161872/hidden-features-of-perl/)

------
vtail
Tricks 1-7 are also supported by Ruby - it accepts the same command-line
options with the same functionality.

------
telemachos
A good collection of articles, tutorials and slide-shows on Perl one-liners,
all in one place:

<http://bufferfly.members.winisp.net/Perl_oneLiners.html>

(I remember especially liking the one from Sial.org - a lot of good Perl
content on that site, the two from Cultured Perl and the FMTYEWTK on mass
edits.)

------
chrismealy
I didn't know about \K and -MRegexp::Common. I liked the explanation of '..'
too. Thanks.

------
absconditus
See also:

<http://petdance.com/perl/command-line-options.pdf>

