Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

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

I'm sorta newish to OO

Okay, I'll admit I've just recently (the last 9 months) been exposted to, and used Object Oriented programming. There are parts of it I like a lot, parts I find stupid or silly, and parts I've yet to understand. This meditation falls under the last category.

 

Is this an unfriendly standard?

It seems that the accepted standard in objects is to bless a hash ref. At the same time, almost everything I read about OO perl exclaims you can use whatever you want as your object. Okay fine. Here's my problem: if you, programmer Xman, write an OO module using a blessed hash ref, and I, theAcolyte, want to subclass/extend/inherit it, I'm effectively tied to using whatever you used, correct? If you have any info stored as $self->{'key'} = value my changing the object type is going to break your code.

Futhermore, if YOU change the underlying implementation of your object, MY code which inherits your object will break. Right?

Have I gotten anything wrong so far?

Next, I just today read a perlmonks post about private variables, and it suggested using a my $var at the top of the package then $obj->{'_myvar'} = \$var; to access it. Same problems I've already listed.

 

I was thinking ...

Wouldn't it make a lot more sense to have a scalar as the default object?

 

Accidental Class Variables

Okay, so you can no longer store instance variables in the object -- but you should be using accessors/mutators anyway, or so I've read. So, how do you deal with instance data? I mulled over this for a while and tried -- based on something I'd seen somewhere -- something like this:

{ my $age; sub age { my $self = shift; return $age unless @_; $age = shift; }}

The problem is, this created a *class* variable (I'm still not sure why) and every instance of the object ended up with the same data.Well, while that solves a completely different problem ... it wasn't what I needed. Eventually I wound up with this:

{ my %agedata; sub age { my $self = shift; return $age{$self} unless @_; $age{$self} = shift; }}

And it seems to work pretty well. Since a reference is a location in memory (err.. I think? Am I right?) each one should be unique ... so we can store *instance* data for each object in a *class* variable. I kinda thought it was neat.

 

Yet Another Oddity

As an aside ... I (again accidently) discovered that if you do:
{{ my $variable } sub accessor { .... }}

That the raw $variable is accessible from anywhere within the package its declared. I have NO idea why. If someone can explain I'd love it. However, anyone outsi de the package -- including a package that inherits it, can't get at $variable and must use the accessor. Not sure if theres any practical purpose for this yet.

 

And, so?

Well, I would think that this type of OO coding would make it much harder for classes inheriting you to break. Also, if you change underlying implementation it won't break things inheriting you as easily. My only wonder is ... what are the major problems with doing it this way? Are there any? That's why I'm posting here... I haven't thought of any yet, but I'm positive the monks around here can set me straight ;-) ... and even if I'm shown this is completely useless it was a fun learning experience.

- theAcolyte

Edit by castaway, swapped font tags for h4 tags


In reply to Why is a hash the default "object" in oo perl? by theAcolyte

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 admiring the Monastery: (5)
As of 2024-03-28 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found