Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
10 tips for writing perl one-liners (ksplice.com)
64 points by nelhage on May 27, 2010 | hide | past | favorite | 18 comments


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.


I don't think HN's sarcasm tags are rendering correctly today.


I think we could really use a Ruby DSL for naysaying.


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.


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

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


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

http://www.catonmat.net/blog/perl-one-liners-explained-part-...


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


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


It's kind of like a gateway drug once you find out about CPAN ;)


It's definitely better than shell for quick file processing :) Check out this fantastic cut replacement (thanks to mjd):

  #!/usr/bin/perl
  
  my $field = shift or usage();
  $field -= 1 if $field > 0;
  
  while (<>) {
      chomp;
      my @f = split;
      print $f[$field], "\n";
  }
  
  sub usage {
      print STDERR "$0 fieldnumber\n";
      exit 1;
  }


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.


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


Well, there are languages I learn to improve my mind, and to improve my craft. As far as the mind goes, perl is nothing new (on the mind to-learn list: Haskell, OCaml, Eiffel, Erlang). Craftwise, it looks very useful.


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

  perl -F, -lane '$t += $F[1]; }{ print $t'
http://www.perlfoundation.org/perl5/index.cgi?eskimo_operato...

`..` 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...

Other features: http://stackoverflow.com/questions/161872/hidden-features-of...


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


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


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





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: