4 ms·
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#BE
by draegtun 16y ago
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-CHECK%2c-INIT-and-END 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 http://feather.perl6.nl/syn/S04.html#Phasers