Not for everyone

Ovid on 2007-04-18T09:25:50

package My::Can;

use strict;
use warnings;
use Sub::Identify 'stash_name';

use base 'Exporter';

our @EXPORT = qw(CAN can);

sub CAN {
    my ( $proto, $method ) = @_;
    $proto->SUPER::can($method);
}

sub can {
    my ( $proto, $method ) = @_;
    return if '_' eq substr $method, 0, 1;   # season to taste

    my $coderef        = $proto->SUPER::can($method) or return;
    my $source_package = stash_name($coderef)        or return;
    return $coderef if $proto->isa($source_package);
}

1;

Update:: smylers just pointed out the bug. Do you see it? (Yes, this can fail with traits and mixins if they're not implemented carefully, but that's not the obvious issue that smylers pointed out).


Bugspotting

Aristotle on 2007-04-18T15:19:44

I assume you mean the fact that SUPER relies on the compile-time package that a function is in.

Re:Bugspotting

Ovid on 2007-04-18T15:34:44

Um, er, uh. No. But now that you mention it, my code is giving me pause. I'll have to look at my tests again. It passes them and I checked that. Now I'm a wee bit confused as to how my tests passed.