Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

classic OO Perl: naming instance variables

by j3 (Friar)
on Apr 11, 2007 at 15:49 UTC ( #609406=perlquestion: print w/ replies, xml ) Need Help??
j3 has asked for the wisdom of the Perl Monks concerning the following question:

While using inheritance with "classic" blessed hashref-based objects, is it generally recommended to name instance variables (the hash keys) using the current class name, say, like $self->{ThisClass__foo}, rather than $self->{foo}?

I read somewhere (can't recall where) that it helps safeguard you from accidentally writing over your parent class's instance variables, and it makes sense (though is admittedly also a bit clumsy).

I tried it out, and it actually helped expose where I'd previously accidentally accessed a parent's instance variable directly. Being familiar with the internals of the parent class (or classes) seems to make this kind of error easy to commit.

(I realize the problem can be avoided altogether using more modern OO techniques (like inside-out objects (or Moose, I suppose)).)

Comment on classic OO Perl: naming instance variables
Re: classic OO Perl: naming instance variables
by bobf (Monsignor) on Apr 11, 2007 at 15:59 UTC

    Damian's Object Oriented Perl recommends prefixing attribute names with the name of the class (Chapter 6, p. 188-189), as you describe. He also discusses Tie::SecureHash as another way to solve this problem (Chapter 11, starting on p. 311).

Re: classic OO Perl: naming instance variables
by friedo (Prior) on Apr 11, 2007 at 15:59 UTC
    In cases where I need to re-bless a hashref from a parent class constructor, I just use a second level hash for my instance data, e.g. $self->{ThisClass}{foo}. I find this easier, because if I have any complicated initialization stuff or just a lot of instance data, I can fiddle around with a separate %data hash and then do $self->{ThisClass} = \%data; when I'm done.
      In cases where I need to re-bless a hashref from a parent class constructor, {snip}

      Just curious, why would you ever need to re-bless a hashref? I've always figured you bless once, using the 2-arg form, in your base class. Inherited classes use $class->SUPER::new(...) to reach back and have the parent do it.

        You're right, you shouldn't normally have to re-bless; I misspoke. On rare occasions, however, I have had to write a class derived from one that used the incorrect form of bless, in which case re-blessing was required.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://609406]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-12-29 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls