Now I have a question of laziness. Having re-arranged your code a bit:
If I add#!/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;
into Utils and Utils::MidLevel, then the $util and $mid objects can both find the foo method.sub foo { Utils::LowLevel::foo }
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:
rather thansub do_highlevel_stuff { my $self = shift; my $value = $self->get_lowlevel_value; # ... }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |