Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found