in reply to Re: Making a variable in a sub retain its value between calls
in thread Making a variable in a sub retain its value between calls
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:
I like using an extra internal INIT block only because it clearly sets the initialization code off from the rest.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
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.
the lowliest monk
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Making a variable in a sub retain its value between calls
by ihb (Deacon) on Apr 18, 2005 at 23:41 UTC | |
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 ihb (Deacon) on Apr 19, 2005 at 16:24 UTC |