rovf has asked for the wisdom of the Perl Monks concerning the following question:
Assuming I have a sub and this sub needs a variable which should have persistent value from call to call, i.e. something like in C++
In Perl I have found the following solution to this problem:// This is C++ void f() { static int myvar=myfunc(); // will be initialized only at the first +call of f() ... g(++myvar); // Incremented on every call }
It is not 100% equivalent to the C++ example, because $myval is not really private to f. If another sub in the same package would also use our $myval, it would denote the same variable.sub f { our $myval; BEGIN { $myval=myfunc() }; ... g(++$myval); }
My questions are now:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Initialization of "local static" (style question)
by Corion (Patriarch) on Feb 03, 2009 at 09:56 UTC | |
by rovf (Priest) on Feb 03, 2009 at 11:19 UTC | |
by massa (Hermit) on Feb 03, 2009 at 12:13 UTC | |
by AnomalousMonk (Archbishop) on Feb 03, 2009 at 18:41 UTC | |
|
Re: Initialization of "local static" (style question)
by JavaFan (Canon) on Feb 03, 2009 at 10:43 UTC | |
by rovf (Priest) on Feb 03, 2009 at 11:21 UTC |