Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found