News for hackers. Stuff that matters.
Re:AWK-ward Ruby by chubot on Tuesday April 26, 2011 @02:09PM (#2486442)
Didn't know Ruby (and Perl?) had BEGIN and END... pretty cool. However I would just write the final example as:

curl -s http://www.gutenberg.org/files/1080/1080.txt | tr ' \t' '\n\n' | sort | uniq -c | sort -n

and avoid any kind of procedural programming.

Re:AWK-ward Ruby by draegtun on Tuesday April 26, 2011 @03:22PM (#2486746)
Didn't know Ruby (and Perl?) had BEGIN and END...

Along with BEGIN & END there is also UNITCHECK, CHECK & INIT in Perl: http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-C...

These named blocks are used under the hood in Perl all the time. For eg.

    foo();  # => bar
    sub foo { say "bar" }
The above works because the last line is interpreted as:

    BEGIN { *foo = sub { say "bar" } }
And so is compiled before the foo() line is reached/run.

PS. Perl6 goes even further and calls them Phasers: http://feather.perl6.nl/syn/S04.html#Phasers

Re:AWK-ward Ruby by rtomayko on Tuesday April 26, 2011 @02:11PM (#2486453)
Me too :) Actually, check out the talk mentioned at the beginning of the essay. Pretty sure I build that exact pipeline at some point near the middle.