5 ms·
10 tips for writing perl one-liners
- pkrumins 16y agoAlso see my article on "Perl One-Liners Explained": http://www.catonmat.net/blog/perl-one-liners-explained-part-one/ http://www.catonmat.net/blog/perl-one-liners-explained-part-...
- sigzero 16y agoI have! I liked it very much. I was using a Perl dos2unix one liner this morning in fact. I love Perl.
- chrismealy 16y agoI didn't know about \K and -MRegexp::Common. I liked the explanation of '..' too. Thanks.
- jrockway 16y agoPerl 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.
- pellicle 16y agoI don't think HN's sarcasm tags are rendering correctly today.
- gamache 16y agoI think we could really use a Ruby DSL for naysaying.
- zavulon 16y agoI 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 16y agoI maintain a lot of Perl. It's a joy. Now this legacy C++ and Java... hoo boy...
- devinj 16y agoI 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).
- someone_here 16y agoIt's kind of like a gateway drug once you find out about CPAN ;)
- sreque 16y agoNo 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 16y agoI 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 http://invisible-island.net/mawk/mawk.html
- devinj 16y agoWell, 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.
- wazoox 16y agoIt'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; }
- vtail 16y agoTricks 1-7 are also supported by Ruby - it accepts the same command-line options with the same functionality.
- telemachos 16y agoA good collection of articles, tutorials and slide-shows on Perl one-liners, all in one place: http://bufferfly.members.winisp.net/Perl_oneLiners.html 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.)
- d0mine 16y ago`END{ }` could be replace by eskimo operator `}{`: perl -F, -lane '$t += $F[1]; }{ print $t' http://www.perlfoundation.org/perl5/index.cgi?eskimo_operator 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-codegolf-gcj-2010-1b-a/2894170#2894170 http://stackoverflow.com/questions/2892126/file-fix-it-codeg... Other features: http://stackoverflow.com/questions/161872/hidden-features-of-perl/ http://stackoverflow.com/questions/161872/hidden-features-of...
- absconditus 16y agoSee also: http://petdance.com/perl/command-line-options.pdf http://petdance.com/perl/command-line-options.pdf
- deleted 16y ago[deleted]