19 comments

[ 3.1 ms ] story [ 34.7 ms ] thread
I didn't know about \K and -MRegexp::Common. I liked the explanation of '..' too. Thanks.
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...

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 ;)
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.
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;
  }
Tricks 1-7 are also supported by Ruby - it accepts the same command-line options with the same functionality.
`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...

(comment deleted)