|Think about Loose Coupling|
Static Variables in Perlby RollyGuy (Chaplain)
|on Mar 30, 2006 at 20:17 UTC||Need Help??|
RollyGuy has asked for the wisdom of the Perl Monks concerning the following question:
A coworker and I are looking at an inheritance problem here at work. We have a particular class used for settings. For our parent class, we have several package variables that correspond to global settings. Children modules would then add specific settings for their instances.
The problem comes into play when we want to access the global or common settings. The functionality we would like is that of C++ static variables where any class can change the variable and it is visible to all.
Is there some way to mimic this functionality in perl?
To be specific, suppose we have a parent class A with setting "knob1". We would access this normally with:
If we have a module B that inherits from A, we would like to be able to change knob1 in the following way:
$B::knob1 = 0;
And have $A::knob1 be automatically updated.
Then we get even more complex with another child of A, say, module C. Module B and Module C are at the same level and share module A as an ancestor. But a change in module C to a globally (inherited) setting should be visible in module B.