go ahead... be a heretic | |
PerlMonks |
Re^3: OO manner of accessing static variables in a subclass?by kcott (Archbishop) |
on Aug 10, 2016 at 22:51 UTC ( [id://1169535]=note: print w/replies, xml ) | Need Help?? |
Firstly, a few words about some OO relationships. From your usage of terms like "parent", "child" and "subclass", I'm not convinced that you've fully grasped their meanings in the OO context.
Now let's change your original class names to more meaningful ones; perhaps Taxidermist and Taxidermy::Data. It should be fairly obvious that Taxidermy::Data is not a Taxidermist: there's no IS-A relationship here. However, a Taxidermist will have Taxidermy::Data; this is a HAS-A relationship and, as such, there are no parent, child or subclasses involved. Going back to your original names, and refactoring the code to completely decouple Monks from Monks::Data, here's how you might implement a HAS-A relationship. (Again, the code is simplistic just to show a technique; production code should include validation, error checking and so on.) In the script (pm_1169455_oo_class_example.pl), I read the name of the data module directly from the command line. This could also be done via options, config files, etc. I've only changed one line:
to
The module Monks::Data is completely unchanged. I've created another data module Monks::DataWeird, to test the decoupling, which looks like this:
The Monks module no longer contains any reference to Monks::Data or, indeed, any Monks::* module. It now looks like this:
Here's three sample runs: two with the real data modules and one with a bogus, non-existent module.
Just to briefly cover your other points: "I'm sort of partial to BEGIN blocks, ..." I have no problem with using BEGIN (or similar) blocks. I didn't use one as I saw no need. See ++hippo's response. Also look at the doco for the our function: it's usage and scoping rules seem to trip up a few users. "Your guess is correct regarding $self->get_attr vs. $self->{attr}. I personally find the latter a lot easier to use, ..." See the code for the Monks module. The "latter" form is only used for initialisation (_init()) and the accessor (_get_data()); elsewhere the accessor is used. Do not use the direct access method in other modules or scripts. Although it's usual to bless a hashref for Perl modules, that's not universal by any means: for example, IO::Handle uses a globref and Class::Std uses a scalarref. Even when the object remains a blessed hashref, the accessor could still change: as a contrived example, instead of $self->{attr}, it could become something like $self->{attr}[USER] // $self->{attr}[DEFAULT]. "Sorry about the furry/fluffy thing, I was just trying to have some fun with my sample code. :) " No need to apologise at all. Changing everything to furry was just expedient for me. I think my initial comments on "OO relationships", and subsequent refactoring, probably cover your other points. [Aside: I'm not requesting you make changes, but for future reference take a look at "What shortcuts can I use for linking to other information?" and "Writeup Formatting Tips". Using links (e.g. parent, instead of 'parent') and marking up inline code (e.g. $self->{attr}, instead of $self->{attr}) can improve readability.] — Ken
In Section
Seekers of Perl Wisdom
|
|