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

Re: On Pseudohashes

Gurusamy Sarathy
January 20, 2000 16:10
Re: On Pseudohashes
Message ID:
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.
># file: $perllib/Class/
>package NEXT;
>        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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About