You only need to change my $var1 ... to our $var1 ... and adapt p2, but maybe your real p1-module is much more complicated?
The patch of p2.pm below seems to work without changing p1.pm on disk, but I wouldn't recommend it for production.
Rather try to fix the root problem, e.g. ask the module author or use a private modified copy of the module and use use lib or $PERL5LIB so the patched version is found first.
#-- p1 not changed on disk ;-) use File::Slurp; my $text = read_file( 'p1.pm' ) ; $text =~ s/^\s*my(\s+\$var1)/our$1/ms; eval $text; #-- above not recommended! package p2; # use p1; our @ISA=qw(p1); sub set_var1 { my $self = shift; my $new_var1 = shift; # $var1 = $new_var1; # return $var1; $p1::var1 = $new_var1; return $p1::var1; } 1;
In reply to Re^4: Can you override lexically scoped variables when sub-classing a module.
by Anonymous Monk
in thread Can you override lexically scoped variables when sub-classing a module.
by learnedbyerror
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |