ysasaki/p5-sub-apply — Issue #2: The 'can' method is not meant to test for subroutine existence
Author: vpit
Created: 2011-11-17 10:20:36+00:00
State: open

By using the 'can' method in '_proc' to check for subroutine existence, you are looking into the whole inheritance graph for a method with the given name, which means that a call to 'apply' may succeed even if there is no such subroutine in the target package :

# This should croak
$ perl -MSub::Apply -wE'{ package X; sub foo { say "X::foo @_" } } { package Y; our @ISA = "X"; Sub::Apply::apply "foo", 1, 2, 3 }'
X::foo 1 2 3

# Ditto
$ perl -MSub::Apply -wE'{ package X; sub foo { say "X::foo @_" } } @Y::ISA = "X"; Sub::Apply::apply "Y::foo", 1, 2, 3'
X::foo 1 2 3

What you really want is something along the lines of :

my $code = do {
no strict 'refs';
my $stash = \%{$package . '::'};
$stash && $stash->{$name} && *{$stash->{$name}}{CODE}
};

Vincent