in reply to Re^4: Making a variable in a sub retain its value between calls
in thread Making a variable in a sub retain its value between calls
Since the other argument wasn't convincing enough, here's an argument that hopefully is.
There's absolutely no reason, in general, for the initialization of the typical static variable to happen at compile time.
Yes there is. Now, a very real-world example:
The output of this will be# Foo.pm package Foo; use base 'Exporter'; our @EXPORT = qw/ foo_init foo_begin /; { my $thing; INIT { $thing = 'init' }; sub foo_init { print "init: $thing"; } } { my $thing; BEGIN { $thing = 'begin' }; sub foo_begin { print "begin: $thing"; } } 1; # Bar.pm package Bar; use Foo; # This can be put in &import if you # think that's more real-world. foo_init(); foo_begin(); 1; # foo.pl #!/usr/bin/perl -wl use Bar;
Use of uninitialized value in concatenation (.) or string at Foo.pm li +ne 9. init: begin: begin
Again, if you put unnecessary INITs in a module you render that module close to useless for many applications where it could've been used.
ihb
See perltoc if you don't know which perldoc to read!
|
---|