Friday, June 08, 2007

Perl monkey

I was building up one of those *nix command pipelines that should never be underestimated (in this case a diff piped to grep piped to...), when I was reaching that threshold at which a one-line command blossoms into a full shell script.

Like a garage tinkerer who reaches for the trusty screwdriver, instead I broke out a Perl one-liner to do the task. A major contributor to the shape of Perl's often-lambasted cryptic syntax is its borrowing from sed, awk, and shell, not to mention the integrated regex (as some have said, Perl is "postmodern" or a "Swiss army chainsaw"). I'm not apologizing for it, but just stating the obvious once again.

Any-who, perl has three parameters that are helpful for these situations: -p for looping over lines from stdin and automatically printing out the final value of the "default variable" $_, -e for specifying the code to execute (within single quotes - use hairy regular expressions or multiple statements separated by ; at your code maintenance peril!), and -a for "auto-splitting" the input line into the list @F. Combine the three and you get "perl -ape". Thank you, my Perl monkey, for helping me get my work done. Even if you can be messy, at least you're trainable.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.