Subject: | AUTOLOADed methods fail 'can' test |
Important to me, perhaps trivial to others. If you run the attached script, you will probably see better than I can explain. I use &AUTOLOAD to catch calls to subroutine/methods that are defined, and in certain circumstances call other subroutines/methods. As in perltoot (I think).
Anyway, such is not caught by the 'can' test, and I presume it can't be unless it is passed a correctly instantiated object (which I guess is the subject of the test).
Anyway, hth.
perl -V is just the latest on Win32 and RedHat. If it turns out to be relevant I'll pass it on.
use strict;
use warnings;
package Example;
our $AUTOLOAD;
sub new {
return bless {}, ref($_[0]) || __PACKAGE__;
}
sub call_hardcoded_sub_as_method {
return 1;
}
sub never_called_directly {
warn '>> This is never_called_directly called by '.join(' ', caller),"\n";
return 1;
}
sub AUTOLOAD {
warn '>> This was autoloaded as '.$AUTOLOAD."\n";
if ($AUTOLOAD eq 'redirect_to_never_called_directly'){
goto &never_called_directly;
}
}
#warn '>> Test calling GOOD autoload method "coltrane" ...'."\n";
#warn '>> ...' . (Example->call_hardcoded_sub_as_method? 'ok':'not ok')."\n";
#warn '>> Test calling BAD autoload method "coltrane" ...'."\n";
#warn '>> ...' . (Example->apriocts? 'not ok':'ok')."\n";
1;
use Test::More tests => 1;
my $eg = Example->new;
isa_ok($eg, 'Example');
can_ok($eg, 'call_hardcoded_sub_as_method');
can_ok($eg->redirect_to_never_called_directly, 'redirect_to_never_called_directly');
# Fake it
$Example::AUTOLOAD = 'redirect_to_never_called_directly';
is(&Example::AUTOLOAD, 1, 'Direct call to AUTOLOAD works');
exit;