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++; }

In reply to Seeking clarification on Inheritance Issues by hesco

Title:
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, details, 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, summary, 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.