in reply to Re^2: Making a variable in a sub retain its value between calls
in thread Making a variable in a sub retain its value between calls
I'm partial to INIT blocks myself
Noooooooooooo! Please please please don't do that! It will break if you put the code using the INIT block in a module and that modules is directly or indirectly required (i.e., loaded after program compile-time). INITs are not executed right after the file in which it was found is done compiling. It's executed after the program file is done compiling. INIT is used when you need to do something delay execution of something to before program run-time.
Personally I go with adding a BEGIN before the bare block, thus avoiding to retype the variable name.
BEGIN { my $memory = 'a'; sub test { print "Value of static var is ", $memory++, "\n"; } }
Update: added clarification.
ihb
See perltoc if you don't know which perldoc to read!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Making a variable in a sub retain its value between calls
by tlm (Prior) on Apr 19, 2005 at 00:11 UTC | |
by ihb (Deacon) on Apr 19, 2005 at 00:24 UTC | |
by tlm (Prior) on Apr 19, 2005 at 00:36 UTC | |
by ihb (Deacon) on Apr 19, 2005 at 00:44 UTC | |
by tlm (Prior) on Apr 19, 2005 at 01:22 UTC | |
| |
by ihb (Deacon) on Apr 19, 2005 at 16:24 UTC |