X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fautouse.pm;h=67c4e01d1dea384b943c9b0cb2896324aec087a4;hb=e82b93481bc82235f35444c372503cc96abe405b;hp=179c382ca294833323f7fa5cc9880c492a2640cb;hpb=4fd801333dcf63040f0a6e1abf20f2d92fe1737a;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/autouse.pm b/lib/autouse.pm index 179c382..67c4e01 100644 --- a/lib/autouse.pm +++ b/lib/autouse.pm @@ -3,7 +3,7 @@ package autouse; #use strict; # debugging only use 5.003_90; # ->can, for my $var -$autouse::VERSION = '1.02'; +$autouse::VERSION = '1.05'; $autouse::DEBUG ||= 0; @@ -39,7 +39,7 @@ sub import { my $closure_import_func = $func; # Full name my $closure_func = $func; # Name inside package - my $index = index($func, '::'); + my $index = rindex($func, '::'); if ($index == -1) { $closure_import_func = "${callpkg}::$func"; } else { @@ -50,10 +50,10 @@ sub import { my $load_sub = sub { unless ($INC{$pm}) { - eval {require $pm}; - die if $@; + require $pm; vet_import $module; } + no warnings 'redefine'; *$closure_import_func = \&{"${module}::$closure_func"}; print "autousing $module; " ."imported $closure_func as $closure_import_func\n" @@ -62,7 +62,8 @@ sub import { }; if (defined $proto) { - *$closure_import_func = eval "sub ($proto) { &\$load_sub }"; + *$closure_import_func = eval "sub ($proto) { goto &\$load_sub }" + || die; } else { *$closure_import_func = $load_sub; } @@ -72,9 +73,10 @@ sub import { sub vet_import ($) { my $module = shift; if (my $import = $module->can('import')) { - croak "autoused module has unique import() method" + croak "autoused module $module has unique import() method" unless defined(&Exporter::import) - && $import == \&Exporter::import; + && ($import == \&Exporter::import || + $import == \&UNIVERSAL::import) } } @@ -95,28 +97,40 @@ autouse - postpone load of modules until a function is used If the module C is already loaded, then the declaration - use autouse 'Module' => qw(func1 func2($;$) Module::func3); + use autouse 'Module' => qw(func1 func2($;$)); is equivalent to use Module qw(func1 func2); -if C defines func2() with prototype C<($;$)>, and func1() and -func3() have no prototypes. (At least if C uses C's -C, otherwise it is a fatal error.) +if C defines func2() with prototype C<($;$)>, and func1() has +no prototypes. (At least if C uses C's C, +otherwise it is a fatal error.) If the module C is not loaded yet, then the above declaration -declares functions func1() and func2() in the current package, and -declares a function Module::func3(). When these functions are called, -they load the package C if needed, and substitute themselves -with the correct definitions. +declares functions func1() and func2() in the current package. When +these functions are called, they load the package C if needed, +and substitute themselves with the correct definitions. + +=begin _deprecated + + use Module qw(Module::func3); + +will work and is the equivalent to: + + use Module qw(func3); + +It is not a very useful feature and has been deprecated. + +=end _deprecated + =head1 WARNING Using C will move important steps of your program's execution from compile time to runtime. This can -=over +=over 4 =item *