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??
The best thing to do is create a accessor and mutator method (or one combined method as seen often in Perl modules) for each of your properties. Users of your objects can only use these to access the property, then you're able to name them whatever you like.
Eh, no. The root of all evil is that there's just one instance variable per object, and that all classes need to stuff their properties in it somehow.

Sure, you can make accessors:

sub oogle { my $self = shift; $self -> {oogle} = shift; # Mark it as very, very, very, do not touch at all private: $self -> {_________________furble} ++; }
But if 10 levels up in the inheritance tree there is an accessor
sub habar { my $self = shift; $self -> {_________________furble} = 0; }
You are still in deep shit. No matter how many underscores you use.

Perls OO model was broken when it was designed. As I said at YAPC It sucks (to which Larry replied "I'm working on it"). Using Perl OO is no fun at all.

Luckely, there is a way to make your properties private while not losing the ability to do inheritance, or pay a costly runtime fee. Use Inside Out Object. Reverse the traditional roles of objects and properties. Let the properties be hashes, and the objects the keys:

package My::Package { my %furble; my %oogle; sub oogle { my $self = shift; $oogle {$self} = shift; $furble {$self} ++; } }
And how your inherited classes are implemented no longer matters. You'll never interfere with their properties, regardless what kind of conventions they use.

Abigail


In reply to Re: Naming convention for Object Variables by Abigail-II
in thread Naming convention for Object Variables by Basilides

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 studying the Monastery: (4)
As of 2024-04-16 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found