Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

(RhetTbull) Re: Replacing namespaces

by RhetTbull (Curate)
on Dec 12, 2001 at 21:43 UTC ( #131311=note: print w/replies, xml ) Need Help??


in reply to Replacing namespaces

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://131311]
help
Chatterbox?
[marto]: good morning all
[Corion]: Hi marto!
[Corion]: The fun show at $work continues, as The Big Project is now in its second week of frantic live-bugfixing and weekend releases where nobody knows what went live. Nothing has been tested anyway.
erix mutters cantankerously under his breath
Corion watches from the sidelines. Or rather, from behind, as my system only gets output from that process and my programs adhere strictly to the GIGO design principle.
[erix]: ah, that's nice to hear Corion :)
[Corion]: erix: Yeah, the sad thing is that all I can do is document things, so I can point fingers when the auditors come :-/
[Corion]: "I'm here to open tickets and point fingers. And I'm all out of tickets."
[erix]: didn't Sybase have pretty good auditing? :) (this is a vague memory)
[erix]: (culprits often are upstream of db of course)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-03-28 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (328 votes). Check out past polls.