Sign in to PrePAN

on::demand Postpone loading a module until it's actually used



use on::demand "My::Module"; # My::Module is not loaded

my $var = My::Module->new(); # require My::Module and jump to its new() method


Deferred loading of modules with large dependency footprint may improve test script and/or command-line tools load times.

Autoload is used. No performance penalty is imposed, aside from loading the module on the first call to it.

Of course, the drawback is that compile time errors (if any) are delayed until unexpected. Better check that the modules in question actually exist and load correctly.


Please note that the standard module collection already has "autouse", which does something similar (or even the same) like this proposed module.
Thanks for mentioning, I was looking for similar modules but that name somehow didn't come up.

It is a different thing, though. It only allows to plug into explicitly listed exported functions, not method calls. Maybe the two can be merged...
For a beginning, one could document how to use autouse with OO modules. It seems that

use autouse 'DateTime' => qw(DateTime::new);

works. And then maybe

use autouse 'DateTime';

which looks like it's a no-op currently, could be implemented to automatically add "${PACKAGE}::new" to the list of functions.

Please sign up to post a review.