develooper Front page | perl.perl5.porters | Postings from January 2000

Re: On Pseudohashes

Thread Previous | Thread Next
From:
Gurusamy Sarathy
Date:
January 20, 2000 16:10
Subject:
Re: On Pseudohashes
Message ID:
200001210011.QAA15809@maul.activestate.com
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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About