Update: In the days and weeks after the debate that followed this comment of mine, through reading and CB conversations I have convinced myself that INIT blocks are not "up to spec", so I'm avoiding them. See in particular TimToady's node in this thread.
In the TIMTOWTDI vein, I'm partial to INIT blocks myself:
use strict;
use warnings;
test() for (1..3);
{
my $memory;
INIT { $memory = 'a' }
sub test {
print "Value of static var is ", $memory++, "\n";
}
}
__END__
Value of static var is a
Value of static var is b
Value of static var is c
I like using an extra internal INIT block only because it clearly sets the initialization code off from the rest.
Of course, if one doesn't like INIT or BEGIN blocks, one can always explicitly check in test that $memory has been initialized and remedy the situation if necessary, but that's ugly, IMO.
|