From: Rafael Garcia-Suarez Date: Mon, 19 Jan 2009 16:37:29 +0000 (+0100) Subject: Upgrade to Module::Load 0.14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=85a8a980a9693eec73613792ab6c1f1c4fdd098d;p=p5sagit%2Fp5-mst-13.2.git Upgrade to Module::Load 0.14 --- diff --git a/lib/Module/Load.pm b/lib/Module/Load.pm index f302c0a..2011955 100644 --- a/lib/Module/Load.pm +++ b/lib/Module/Load.pm @@ -1,6 +1,6 @@ package Module::Load; -$VERSION = '0.12'; +$VERSION = '0.14'; use strict; use File::Spec (); @@ -30,18 +30,17 @@ sub load (*;@) { die $err if $err; } } - __PACKAGE__->_export_to_level(1, $mod, @_) if @_; -} - -### 5.004's Exporter doesn't have export_to_level. -### Taken from Michael Schwerns Test::More and slightly modified -sub _export_to_level { - my $pkg = shift; - my $level = shift; - my $mod = shift; - my $callpkg = caller($level); - - $mod->export($callpkg, @_); + + ### This addresses #41883: Module::Load cannot import + ### non-Exporter module. ->import() routines weren't + ### properly called when load() was used. + { no strict 'refs'; + my $import; + if (@_ and $import = $mod->can('import')) { + unshift @_, $mod; + goto &$import; + } + } } sub _to_file{ diff --git a/lib/Module/Load/t/01_Module-Load.t b/lib/Module/Load/t/01_Module-Load.t index f811447..0aaed74 100644 --- a/lib/Module/Load/t/01_Module-Load.t +++ b/lib/Module/Load/t/01_Module-Load.t @@ -1,68 +1,52 @@ ### Module::Load test suite ### -BEGIN { +BEGIN { if( $ENV{PERL_CORE} ) { chdir '../lib/Module/Load' if -d '../lib/Module/Load'; unshift @INC, '../../..'; } -} +} BEGIN { chdir 't' if -d 't' } use strict; use lib qw[../lib to_load]; use Module::Load; -use Test::More tests => 13; - - -{ - my $mod = 'Must::Be::Loaded'; - my $file = Module::Load::_to_file($mod,1); - - eval { load $mod }; - - is( $@, '', qq[Loading module '$mod'] ); - ok( defined($INC{$file}), q[... found in %INC] ); -} - -{ - my $mod = 'LoadMe.pl'; - my $file = Module::Load::_to_file($mod); - - eval { load $mod }; - - is( $@, '', qq[Loading File '$mod'] ); - ok( defined($INC{$file}), q[... found in %INC] ); -} - -{ - my $mod = 'LoadIt'; - my $file = Module::Load::_to_file($mod,1); +use Test::More 'no_plan'; - eval { load $mod }; +### test loading files & modules +{ my @Map = ( + # module flag diagnostic + [q|Must::Be::Loaded|, 1, 'module'], + [q|LoadMe.pl|, 0, 'file' ], + [q|LoadIt|, 1, 'ambiguous module' ], + [q|ToBeLoaded|, 0, 'ambiguous file' ], + ); - is( $@, '', qq[Loading Ambigious Module '$mod'] ); - ok( defined($INC{$file}), q[... found in %INC] ); -} + for my $aref (@Map) { + my($mod, $flag, $diag) = @$aref; -{ - my $mod = 'ToBeLoaded'; - my $file = Module::Load::_to_file($mod); + my $file = Module::Load::_to_file($mod, $flag); - eval { load $mod }; + eval { load $mod }; - is( $@, '', qq[Loading Ambigious File '$mod'] ); - ok( defined($INC{$file}), q[... found in %INC] ); + is( $@, '', qq[Loading $diag '$mod' $@] ); + ok( defined($INC{$file}), qq[ '$file' found in \%INC] ); + } } ### Test importing functions ### { my $mod = 'TestModule'; my @funcs = qw[func1 func2]; - + eval { load $mod, @funcs }; is( $@, '', qq[Loaded exporter module '$mod'] ); - + + ### test if import gets called properly + ok( $mod->imported, " ->import() was called" ); + + ### test if functions get exported for my $func (@funcs) { - ok( $mod->can($func), "$mod -> can( $func )" ); - ok( __PACKAGE__->can($func), "we -> can ( $func )" ); - } -} + ok( $mod->can($func), " $mod->can( $func )" ); + ok( __PACKAGE__->can($func), " we ->can ( $func )" ); + } +} diff --git a/lib/Module/Load/t/to_load/TestModule.pm b/lib/Module/Load/t/to_load/TestModule.pm index bf73343..bc18a03 100644 --- a/lib/Module/Load/t/to_load/TestModule.pm +++ b/lib/Module/Load/t/to_load/TestModule.pm @@ -2,14 +2,18 @@ package TestModule; use strict; require Exporter; -use vars qw(@EXPORT @EXPORT_OK @ISA); +use vars qw(@EXPORT @EXPORT_OK @ISA $IMPORTED); -@ISA = qw(Exporter); -@EXPORT = qw(func2); -@EXPORT_OK = qw(func1); +@ISA = qw(Exporter); +@EXPORT = qw(func2); +@EXPORT_OK = qw(func1); -sub func1 { return "func1"; } +### test if import gets called properly +sub import { $IMPORTED = 1; goto &Exporter::import; } +sub imported { $IMPORTED; } -sub func2 { return "func2"; } +sub func1 { return "func1"; } + +sub func2 { return "func2"; } 1;