AWK-ward Ruby
tomayko.com
tomayko.com
[1] http://cm.bell-labs.com/cm/cs/awkbook (This link seems dead at the moment. I'm very much going to hope that's temporary.)
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.
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
my $block = sub { say "w00t, blocks!" };
my $closure = sub { $block->(); say "w00t, closures!" };
You'll have to look further for something Ruby does that Perl doesn't :)Article just acted like that was a ruby invention (which they corrected)
My comment had nothing to do with perl.
my $block = { say "w00t, blocks!" };
And there are also pointy blocks for neater stuff: my $pointy_block = -> $text { say "$text, blocks" };
$pointy_block("ne@t");For what it's worth (you may know this but maybe not everyone does), Ruby also provides the -i flag.
Example of AWK usage from my old (currently unmaintained! and insecure!) pcspk project (http://download.przemoc.net/pcspk) is "Siemens ringtone converter": https://gist.github.com/943386 (actually it requires gawk, which has nice extensions)
"print $1" was the most I ever used AWK. So when I read the first part (whose examples made great sense to me) I shouted "AWK is awesome! I should learn it now!". Then the last part came in and showed me how I could use a language I already know to precisely replace it. I both feel compelled that I can leverage my current knowledge right now to solve another class of problems, and sad not to learn a completely new thing.
On closer examination I noticed the video is here http://confreaks.net/system/assets/datas/1177/original/363-g...
So 'ruby -n' acts like Awk? Neat.