Poor man's AutoLoader

For some reason that I have not bothered to investigate yet (but probably due to my screwed up perlbrew installation), AutoLoader fails to work with 'prove' and 'dzil test'. I just need to separate a couple of heavy routines off the main module, to shave startup time. So here's what I did:

1) Manually split the heavy routines to My/Module/al_method1.pm and My/Module/al_method2.pm.

2) In My/Module.pm, add:

use vars qw($AUTOLOAD);
my ($pkg, $sub) = $AUTOLOAD =~ /(.+)::(.+)/;
die "Undefined subroutine" unless $sub =~ /^(method1|method2)$/;
$pkg =~ s!::!/!g;
require "$pkg/al_$sub.pm";
goto &$AUTOLOAD;

So far it works (some tweak might be needed under -T). And I don't have to deal with AutoSplit. I just hope there's no gaping hole somewhere.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.