[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Re: On Pseudohashes
On Thu, 20 Jan 2000 17:28:50 +1100, Damian Conway wrote:
>Previously I wrote:
> > In fact, this capacity is useful (some might say "essential") for
> > *all* dynamically dispatched functions, not just those named
> > AUTOLOAD. One ought to be able to write:
> >
> > return $self->NEXT::m();
> >
> > where NEXT is analogous to SUPER, but resumes the current dispatch
> > search, rather than initiating a new dispatch.
>
>And here's the implementation. Feedback most welcome.
>
>Damian
>
>-----------cut-----------cut-----------cut-----------cut-----------cut----------
>
># file: $perllib/Class/Redispatch.pm
>
>package NEXT;
>
>sub AUTOLOAD
>{
> my $self = shift;
> my $caller = (caller(1))[3];
> my ($class,$method) = $caller =~ m{(.*)::(.*)}g;
> my $refclass = ref $self;
> if ($class ne $refclass and defined &{$refclass."::".$method}) {
> eval qq{ local *$caller; package $refclass;
> \$self->SUPER::$method(\@_)
> if \$self->can('$method'); }
> }
> else {
> eval qq{ local *$caller;
> \$self->$method(\@_)
> if \$self->can('$method'); }
> }
>}
I suspect this is going to be horribly expensive, because every NEXT::foo()
call will wipe out the method cache.
There may be a better ways. For example, for every package in the
inheritance tree that declares a CAN() method, that will be invoked with
the method being searched for along with its arguments. CAN() can then
return a coderef/false value to determine if there exists a method to
be called in that package. Of course, UNIVERSAL::can() would simply
call CAN() if it exists. It would be possible to make this scheme fairly
efficient by keeping a bit on every stash where a CAN() method was seen
at compile time.
Sarathy
gsar@ActiveState.com
- References to:
-
Damian Conway <damian@cs.monash.edu.au>
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]