Okay, this clears a bit of the mist in my head. Blessing into whatever class I want is certainly, err, what I want. Your example of having an init() method is something I'll have to incorporate into my code; you're right on it being easily understandable. I also notice that you went with Utils and Utils::LowLevel rather than my Utils and LowLevelUtils - obviously I'm still learning the basics of designing a useful package hierarchy.

Now I have a question of laziness. Having re-arranged your code a bit:

#!/usr/bin/perl { package Utils; sub new { my $class = shift; my $self = {}; # Bless into the appropriate class my $obj = bless $self, $class; $obj->init(@_); return $obj; } sub init { # does nothing } 1; } { package Utils::MidLevel; @Utils::MidLevel::ISA = qw(Utils); sub init { my $self = shift; my @args = @_; $self->SUPER::init(@args); # Now do initialization of object } 1; } { package Utils::LowLevel; @Utils::LowLevel::ISA = qw(Utils::MidLevel); sub init { my $self = shift; my @args = @_; $self->SUPER::init(@args); # Now do initialization of this class } sub foo { print "foo\n" } 1; } print "Creating new Utils object\n"; my $util = new Utils; #$util->foo(); # can't find method :( print "\n"; print "Creating new Utils::MidLevel object\n"; my $mid = new Utils::MidLevel; #$mid->foo(); # can't find method :( print "\n"; print "Creating new Utils::LowLevel object\n"; my $low = new Utils::LowLevel; $low->foo(); print "\n"; exit;
If I add
sub foo { Utils::LowLevel::foo }
into Utils and Utils::MidLevel, then the $util and $mid objects can both find the foo method.

Now, if I'm pig-headed and am dead set against adding those kinds of stubs into Utils and Utils::MidLevel and similarly want to have Utils and Utils::MidLevel be able to access lower-level methods in this manner:

sub do_highlevel_stuff { my $self = shift; my $value = $self->get_lowlevel_value; # ... }
rather than

sub do_highlevel_stuff { my $self = shift; my $value = Utils::LowLevel::get_lowlevel_value($self); # ... }

Well - can I have my cake and eat it too? That is, can I keep Utils aware of methods in Utils::MidLevel aware of methods in Utils::LowLevel without having to go through double-colon acrobatics (plus calling methods as regular functions)?

I'm guessing the answer to the above question is no; without stubs and without calling the methods functionally, there is no way to do what I want - just thought I'd ask on the off chance that there's a crafty way to do this. :)

Sorry if this post is hard to read due to incorrect usage of OO terminology (or other CompSci terminology for that matter)

(Good catch on extending Utils with Utils::MidLevel, btw. I have been thinking, "Gee, this really isn't really a low-level table action, nor a high-level action... it's more mid-level...")

blyman
setenv EXINIT 'set noai ts=2'


In reply to Re: Re: Faking new() method in high-level package by belden
in thread Faking new() method in high-level package by belden

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.