Oh Monks, I am in need of your assistance again. I am trying to sub-class a module that makes of a lexical (my) variable set at the package level. I need to change the change the value of this variable in my module; however, I cannot figure out how to do this.
The following code is representative of the other module and what I would like to do.
package p1; my $var1 = 'var1-p1'; sub new { my $class = ref $_[0] ? ref shift : shift; my $self = {}; bless $self, $class; return $self; } sub get_var1 { return $var1; } 1;
My desired approach - which won't work as shown - is something like this:
package p2; use p1; our @ISA=qw(p1); sub set_var1 { my $self = shift; my $new_var1 = shift; $var1 = $new_var1; return $var1; } 1;
An example of using these modules is:
use p2; my $obj = p2->new(); say $obj->get_var1() . "\n"; say $obj->set_var1('changed') . "\n"; say $obj->get_var1() . "\n"
With the desired output being:
var-p1 changed changed
Any assistance in figuring out a way around this conundrum short of modifying the first module would be greatly appreciated. Having said that, I am trying to to get the first module modified to include constructors for changing this variable.
As always, thanks in advance for your assistance!
lbe
In reply to Can you override lexically scoped variables when sub-classing a module. by learnedbyerror
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |