Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm quite new at OO and OO Perl so I may not quite grasp the problem but this looks like a good use of TheDamian's NEXT module. It basically allows your object to dispatch a method to all other objects in the hierarchy. The following code shows an example of how this might be done.
#!/usr/bin/perl use warnings; use strict; use NEXT; package Base; sub new { print "Base new\n"; my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self,$class); return $self; } sub Base::method { my $self = shift; my $data = shift || 0; print "$self: Base method, data = $data\n"; return 'Base'; } sub Base::DESTROY { my $self = shift; print "$self: Base DESTROY\n"; } package A; use base qw(Base); sub new { print "A new\n"; my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self,$class); return $self; } sub A::method { my $self = shift; my $data = shift || 0; print "$self: A method, data = $data\n"; return 'A' if $data =~ /A/; #it's my data, so handle it $self->NEXT::ACTUAL::method($data); #otherwise dispatch to the nex +t class } sub A::DESTROY { my $self = shift; print "$self: A DESTROY\n"; $self->NEXT::UNSEEN::DESTROY(); } package B; use base qw(A); sub new { print "B new\n"; my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self,$class); return $self; } sub B::method { my $self = shift; my $data = shift || 0; print "$self: B method, data = $data\n"; return 'B' if $data =~ /B/; #it's my data, so handle it $self->NEXT::ACTUAL::method($data); #otherwise, dispatch to next c +lass; } sub B::DESTROY { my $self = shift; print "$self: B DESTROY\n"; $self->NEXT::UNSEEN::DESTROY(); } package main; my $obj = new B(); #this one will be handled by Base print "calling method with generic data\n"; my $return = $obj->method('foo data'); print "return value was $return\n"; #this one will get handled by A print "calling method with A's data\n"; $return = $obj->method('A data'); print "return value was $return\n"; #this one will get handled by B print "calling method with B's data\n"; $return = $obj->method('B data'); print "return value was $return\n";
When run this produces:
B new calling method with generic data B=HASH(0xa01116c): B method, data = foo data B=HASH(0xa01116c): A method, data = foo data B=HASH(0xa01116c): Base method, data = foo data return value was Base calling method with A's data B=HASH(0xa01116c): B method, data = A data B=HASH(0xa01116c): A method, data = A data return value was A calling method with B's data B=HASH(0xa01116c): B method, data = B data return value was B B=HASH(0xa01116c): B DESTROY B=HASH(0xa01116c): A DESTROY B=HASH(0xa01116c): Base DESTROY
Read the NEXT docs for details on how this works and the UNSEEN and ACTUAL parameters.

In reply to (RhetTbull) Re: Replacing namespaces by RhetTbull
in thread Replacing namespaces by smferris

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (7)
    As of 2021-04-15 18:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?