Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

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

isn't there a danger $self->{'JOKE'} is already in use?

Indeed. One way of avoiding this is to use your own package name in the hash key, to ensure a little more uniqueness:

my $package = __PACKAGE__ . ':'; sub set_joke { my $self = shift; $self->{$package.'JOKE'} = shift; } sub get_joke { my $self = shift; return $self->{$package.'JOKE'}; } sub say_joke { my $self = shift; print $self->get_joke, "\n"; }

This technique can only get you so far though - with some classes it's not enough, so the inside-out object technique explained by JavaFan is necessary.

As an example, imagine an EmailHeaders class where each hash key represents an e-mail header (To, From, Subject, etc). You want to subclass it and store some additional information which is not an e-mail header. But any key you add to the hash will be interpreted as another e-mail header by the parent class. Here you really would need to use inside-out objects.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: avoiding overwriting variables in an inherited class by tobyink
in thread avoiding overwriting variables in an inherited class by whatnext156

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 musing on the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found