Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (6)
    As of 2021-01-17 16:50 GMT
    Find Nodes?
      Voting Booth?