cpan/Module-Load-Conditional/t/01_Module_Load_Conditional.t Module::Conditional tests
cpan/Module-Load-Conditional/t/test_lib/a/X.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/test_lib/b/X.pm Module::Conditional tests
+cpan/Module-Load-Conditional/t/to_load/AutoLoad.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/Commented.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/InPod.pm Module::Load::Conditional tests
cpan/Module-Load-Conditional/t/to_load/LoadIt.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/LoadMe.pl Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/MustBe/Loaded.pm Module::Load::Conditional tests
+cpan/Module-Load-Conditional/t/to_load/NotAutoLoad.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/NotMain.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/NotX.pm Module::Conditional tests
cpan/Module-Load-Conditional/t/to_load/ToBeLoaded Module::Conditional tests
use strict;
-use Module::Load;
+use Module::Load qw/load autoload_remote/;
use Params::Check qw[check];
use Locale::Maketext::Simple Style => 'gettext';
$FIND_VERSION $ERROR $CHECK_INC_HASH];
use Exporter;
@ISA = qw[Exporter];
- $VERSION = '0.58';
+ $VERSION = '0.60';
$VERBOSE = 0;
$DEPRECATED = 0;
$FIND_VERSION = 1;
return $href;
}
-=head2 $bool = can_load( modules => { NAME => VERSION [,NAME => VERSION] }, [verbose => BOOL, nocache => BOOL] )
+=head2 $bool = can_load( modules => { NAME => VERSION [,NAME => VERSION] }, [verbose => BOOL, nocache => BOOL, autoload => BOOL] )
C<can_load> will take a list of modules, optionally with version
numbers and determine if it is able to load them. If it can load *ALL*
solve a problem in a program, and only wish to continue down a path
if all modules could be loaded, and not load them if they couldn't.
-This function uses the C<load> function from Module::Load under the
-hood.
+This function uses the C<load> function or the C<autoload_remote> function
+from Module::Load under the hood.
C<can_load> takes the following arguments:
already failed to load before. By default, C<can_load> will check its
cache, but you can override that by setting C<nocache> to true.
+=item autoload
+
+This controls whether imports the functions of a loaded modules to the caller package. The default is no importing any functions.
+
+See the C<autoload> function and the C<autoload_remote> function from L<Module::Load> for details.
+
=cut
sub can_load {
modules => { default => {}, strict_type => 1 },
verbose => { default => $VERBOSE },
nocache => { default => 0 },
+ autoload => { default => 0 },
};
my $args;
if ( $CACHE->{$mod}->{uptodate} ) {
- eval { load $mod };
+ if ( $args->{autoload} ) {
+ my $who = (caller())[0];
+ eval { autoload_remote $who, $mod };
+ } else {
+ eval { load $mod };
+ }
### in case anything goes wrong, log the error, the fact
### we tried to use this module and return 0;
);
}
+# test for autoload
+
+# autoload
+{
+ my $use_list = { 'AutoLoad' => 0 };
+ my $bool = can_load( modules => $use_list, autoload => 1 );
+ ok( $bool, q[autoloaded] );
+
+ eval { func1(); };
+ is( $@, '', q[exported function] );
+
+ eval { func2(); };
+ ok( $@, q[not exported function] );
+}
+
+# not autoload
+{
+ my $use_list = { 'NotAutoLoad' => 0 };
+ my $bool = can_load( modules => $use_list );
+ ok( $bool, q[not autoloaded] );
+
+ eval { func3(); };
+ ok( $@, q[not exported function - func3] );
+
+ eval { func4(); };
+ ok( $@, q[not exported function - func4] );
+}
+
### test 'requires' ###
SKIP:{