hesco has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: I can certainly relate to what Micheal Schwern said about having spent so long trying to understand a little about objects, like what I write below, if not enough to actually use it, then perhaps enough to percieve conceptually what it might be about, that now I would have a lot to unlearn to wrap my head around Moose.pm. I'm afraid I might have to leave that for another day, as I feel I'm too deeply immersed already in my current project, to risk switching approaches at this point, however imperfect my current approach might be.

So someone on the cb pointed me at the perldoc for base, which describes itself as "Establish an ISA relationship with base classes at compile time". I'm guessing that this means that when I write:

package MyPackage::Module::ThisFeature; use base MyPackage::Module;
that what I'm saying is that were I to write a test script reading:

use Test::More 'no_plan'; my $obj = MyPackage::Module::ThisFeature->new(); isa_ok($obj,'MyPackage::Module'); isa_ok($obj,'MyPackage::Module::ThisFeature'); isa_ok($obj,'MyPackage::Module::ThatFeature');
that my first test would pass, but my second and third one would fail. Is that right?

But what I actually got was:

perl 01-test.t ok 1 - The object isa MyPackage::Module ok 2 - The object isa MyPackage::Module::ThisFeature not ok 3 - The object isa MyPackage::Module::ThatFeature
So I guess my object belongs to both its own class, as well as its base class, but not to a sister class, then.

I'm getting to a point where this conceptual stuff is starting to break down for me. I think I'll start another thread on SoPW to try to untangle the the real world issue I'm struggling with now.


OK, inheritance is so far to me an untested feature. But I think its far past time that I gain some comfort with it. I've been coding with perl long enough without having a handle on this one. I'm watching a module I'm writing become overly complex and I think providing for inheritance between my classes might simplify things. I'm seeking feedback and clarity on the following approach, as well as any links to documentation which might help me better understand all of this.

In my brief example below, if I understand this correctly, the "use base MyPackage::Module;" line permits both the MyPackage::Module::ThisFeature and the MyPackage::Module::ThatFeature to "inherit" methods from the MyPackage::Module. So that a script which reads:

#!/usr/bin/perl -wT use strict; use warnings; use MyPackage::Module::ThisFeature; use MyPackage::Module::ThatFeature; my $obj = MyPackage::Module::ThisFeature->new(); my $result = $obj->this_method(); . . .
will instantiate an object belonging to the "MyPackage::Module" class, which can use any of its class methods, as well as methods made available in the MyPackage::Module::ThisFeature package, except that the base class' ->this_method() method will be over-ridden by the version provided in the MyPackage::Module::ThisFeature package. Do I have this right so far?

Is there some way to access methods in MyPackage::Module::ThatFeature, short of fully qualifying them as: MyPackage::Module::ThatFeature->still_another_method()?

Am I lost on this yet?

-- Hugh

package MyPackage::Module; sub new { . . . bless $self, $class; } sub this_method { } sub that_method { } 1; package MyPackage::Module::ThisFeature; use base MyPackage::Module; sub this_method { } sub some_other_method { } 1; package MyPackage::Module::ThatFeature; use base MyPackage::Module; sub that_method { } sub still_another_method { } 1;
if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Seeking clarification on Inheritance Issues
by pc88mxer (Vicar) on May 23, 2008 at 06:18 UTC
    You basically have it right.
    Is there some way to access methods in MyPackage::Module::ThatFeature, short of fully qualifying them as: MyPackage::Module::ThatFeature->still_another_method()?
    To explicitly call a method in a superclass, fully qualify the method name with its package:
    # $obj is an object which is in a subclass of MyPackage::Module::ThatF +eature $obj->MyPackage::Module::ThatFeature::method_name(...);
    If you don't know the name of the superclass, you can use the SUPER pseudo-class:
    $obj->SUPER::method_name(...)
    Have a look at perldoc perlbot for more good stuff on perl objects.
Re: Seeking clarification on Inheritance Issues
by dragonchild (Archbishop) on May 23, 2008 at 13:42 UTC
    "Parent class" and "Child class" are really bad terms. The best way to think about it, imho, is in terms of generalized vs. specialized. A "child class" is the specialization of the "parent class". You don't create a massive OO system for only one set of behaviors. You create it when you have several sets of behaviors that share a lot of common behaviors. The generalized case (parent class) provides the common behaviors. Then, each set of behaviors adds its own bits in a specialized case (child class).

    What you're talking about regarding ThisFeature vs. ThatFeature sounds more like roles, honestly. Read ALL the documentation for Moose. That will probably help a lot. Oh, and when you decide to build a big OO system, use Moose. It will save you a lot of heartache.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Seeking clarification on Inheritance Issues
by stvn (Monsignor) on May 23, 2008 at 18:38 UTC

    You might want to watch the recent talk Micheal Schwern gave at YAPC::Asia on this exact topic.

    -stvn

        The video is actually embedded in the page, I think it is a flash player perhaps. You have to be sure to click the "play" button though, clicking anywhere else will get you to what you linked too.

        -stvn