in reply to Some trouble with closures
Suppose you have a method of a class that you write using Moose(but a plain Perl object is also relevant), and you use inside that method a static variable (static in the C sense, by that I mean a variable that has scope local to the method but a lifetime that spans each call of said method). I was recommended that I use a closure and enclose the declaration of that variable and the method inside the closure to cause the variable to be static(again, the C sense of static).My recommendation would be to declare the variable with the static keyword inside the sub you want to static variable.
give up the closure and the static(in C sense) variable and go for an attribute in the class that would be used for this, which if you use Moose has the advantage of being able to specify default => value , so the attribute is set to that value upon constructing a new objectHuh? What? Now I'm confused. Do you want a static variable, or not? If it's static (as in the C sense; and in the Perl sense), there's just one. If you want a different one per object, you don't have static variables; you have your classic, run-of-the-mill, 14-in-a-dozen, object attribute. But C doesn't have objects, so if it's "in the C sense", it cannot be related to objects, can it?
Perhaps you should explain what you want this variable to be.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Some trouble with closures
by DStaal (Chaplain) on Feb 17, 2010 at 13:51 UTC | |
by JavaFan (Canon) on Feb 17, 2010 at 18:29 UTC | |
by DStaal (Chaplain) on Feb 17, 2010 at 19:26 UTC | |
by Anonymous Monk on Feb 19, 2010 at 19:25 UTC | |
by JavaFan (Canon) on Feb 19, 2010 at 23:26 UTC |