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

Breadth-first method dispatch

by blokhead (Monsignor)
on Jan 30, 2004 at 20:48 UTC ( [id://325389]=CUFP: print w/replies, xml ) Need Help??

Decorator(?) classes and inheritance made me wonder how tough it would be to get breadth-first method dispatch. While a truly robust solution would involve hacking internals, you can do some interesting things still in pure Perl.

Disclaimer: All code is proof-of-concept and probably breaks in many curious cases (cyclic isa-hierarchy for one). If anyone might find this useful, I can package it up and CPAN it.

If all you want is something like SUPER that acts in a breadth-first-search way, you can use something like this: (unfortunately overriding the SUPER namespace doesn't work -- it's more magical than that)
package SUPERB; use Carp; sub AUTOLOAD { my $self = shift; my $caller = caller; $AUTOLOAD =~ s/.*:://; for my $method ($AUTOLOAD, "AUTOLOAD") { my @queue = @{"$caller\::ISA"}; while (@queue) { my $class = shift @queue; if ( *{"$class\::$method"}{CODE} ) { my $call = "$class\::$AUTOLOAD"; return $self->$call(@_); } else { push @queue, @{"$class\::ISA"}; } } } my $class = ref $self || $self; croak qq[Can't locate object method "$AUTOLOAD" via package "$clas +s"]; } 1;
Now we can achieve this:
use SUPERB; package Base; sub foo { print "Base::foo\n"; } sub bar { print "Base::bar\n"; } sub baz { print "Base::baz\n"; } package A; @ISA = qw/Base/; sub foo { print "A::foo\n"; } package B; @ISA = qw/Base/; sub bar { print "B::bar\n"; } package C; @ISA = qw/A B/; sub foo { print "C::foo\n"; shift->SUPERB::foo; } sub bar { print "C::bar\n"; shift->SUPERB::bar; } sub baz { print "C::baz\n"; shift->SUPERB::baz; } package main; my $c = bless [], 'C'; $c->foo; $c->bar; $c->baz; __OUTPUT__ C::foo A::foo C::bar B::bar C::baz Base::baz
The important thing to notice is that SUPERB::bar correctly dispatches to B::bar instead of Base::Bar.

Another variant on this is to have the actual @ISA hierarchy searched in a BFS way, for when you don't use SUPER -- For example, in the code above, say we removed all the subs in the "C" package (maybe we don't need to override them at all in this subclass) and still wanted to get B::bar instead of Base::bar.

To do this, you have to use your own mechanism instead of Perl's @ISA hierarchy. You can use AUTOLOAD to dispatch the calls up the hierarchy however you want. The problem is that Perl won't call the subclass' AUTOLOAD if the actual method exists higher up. To get around this, you can not declare @ISA in the subclass and use a private list of isa-classes. This way, AUTOLOAD gets called right away if the method is unimplemented in the subclass. This little package provides a simple way to do it:

package bfs_dispatch; use Carp; sub import { my ($x, @isa) = @_; my $caller = caller; *{"$caller\::AUTOLOAD"} = sub { my $self = shift; $AUTOLOAD =~ s/.*:://; for my $method ($AUTOLOAD, "AUTOLOAD") { my @queue = @isa; while (@queue) { my $class = shift @queue; if ( my $code = *{"$class\::$method"}{CODE} ) { my $call = "$class\::$AUTOLOAD"; return $self->$call(@_); } else { push @queue, @{"$class\::ISA"}; } } } my $class = ref $self || $self; croak qq[Can't locate object method "$AUTOLOAD" via package "$ +class"]; }; } 1;
You call it from within a package like this: use bfs_dispatch qw/base1 base2/;, with the isa information provided. So in the code example above, we'd replace:
## replace this: package C; @ISA = qw/A B/; sub foo { print "C::foo\n"; shift->SUPERB::foo; } sub bar { print "C::bar\n"; shift->SUPERB::bar; } sub baz { print "C::baz\n"; shift->SUPERB::baz; } ## with this: package C; use bfs_dispatch qw/A B/; ## methods deleted, lets see if the method dispatcher finds the correc +t one!
Then when we run that code, we get what we expect:
__OUTPUT__ A::foo B::bar Base::baz
There are problems with this second approach that may be obvious to you: SUPER (and SUPERB) won't work because there is no @C::ISA. Also UNIVERSAL::isa and UNIVERSAL::can will be equally confused. Thus, this is probably a very bad idea. If you want the behavior of BFS method dispatch, you are probably better off declaring all the methods in the subclass like this:
package C; @ISA = qw/A B/; sub foo { $_[0]->SUPERB::foo(@_[1..$#_]); } sub bar { $_[0]->SUPERB::bar(@_[1..$#_]); } sub baz { $_[0]->SUPERB::baz(@_[1..$#_]); }
Although this approach screams maintainability problems as well.

blokhead

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://325389]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found