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