Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: Can you override lexically scoped variables when sub-classing a module.

by Anonymous Monk
on Oct 11, 2012 at 20:45 UTC ( #998542=note: print w/replies, xml ) Need Help??


in reply to Re^3: Can you override lexically scoped variables when sub-classing a module.
in thread Can you override lexically scoped variables when sub-classing a module.

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998542]
help
Chatterbox?
[virtualsue]: 1nickt thatch repair just finishing
[virtualsue]: they've done enough to get it through winter
holli loves lightning talks
[1nickt]: ... but not the last straw?
1nickt ducks
virtualsue will be sweeping/raking it up for days
[virtualsue]: I need to get some cat pics into my talk http://www.cat- lovers-only.com/ images/three-cat- burritos-to-go-500 .jpg

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-11-21 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (299 votes). Check out past polls.

    Notices?