autouse.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
88319179998c5d8d19943d09450931b88694be58takashipackage autouse;
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi#use strict; # debugging only
88319179998c5d8d19943d09450931b88694be58takashiuse 5.003_90; # ->can, for my $var
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi$autouse::VERSION = '1.03';
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi$autouse::DEBUG ||= 0;
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashisub vet_import ($);
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
2e545ce2450a9953665f701bb05350f0d3f26275ndsub croak {
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen require Carp;
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen Carp::croak(@_);
88319179998c5d8d19943d09450931b88694be58takashi}
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashisub import {
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen my $class = @_ ? shift : 'autouse';
3f08db06526d6901aa08c110b5bc7dde6bc39905nd croak "usage: use $class MODULE [,SUBS...]" unless @_;
88319179998c5d8d19943d09450931b88694be58takashi my $module = shift;
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi (my $pm = $module) =~ s{::}{/}g;
3f08db06526d6901aa08c110b5bc7dde6bc39905nd $pm .= '.pm';
88319179998c5d8d19943d09450931b88694be58takashi if (exists $INC{$pm}) {
88319179998c5d8d19943d09450931b88694be58takashi vet_import $module;
88319179998c5d8d19943d09450931b88694be58takashi local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
88319179998c5d8d19943d09450931b88694be58takashi # $Exporter::Verbose = 1;
88319179998c5d8d19943d09450931b88694be58takashi return $module->import(map { (my $f = $_) =~ s/\(.*?\)$//; $f } @_);
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung }
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi # It is not loaded: need to do real work.
88319179998c5d8d19943d09450931b88694be58takashi my $callpkg = caller(0);
88319179998c5d8d19943d09450931b88694be58takashi print "autouse called from $callpkg\n" if $autouse::DEBUG;
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi my $index;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf for my $f (@_) {
ecdc51f25765b81b6d07389c6fa02a9529411f5drbowen my $proto;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf $proto = $1 if (my $func = $f) =~ s/\((.*)\)$//;
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi my $closure_import_func = $func; # Full name
88319179998c5d8d19943d09450931b88694be58takashi my $closure_func = $func; # Name inside package
88319179998c5d8d19943d09450931b88694be58takashi my $index = rindex($func, '::');
88319179998c5d8d19943d09450931b88694be58takashi if ($index == -1) {
88319179998c5d8d19943d09450931b88694be58takashi $closure_import_func = "${callpkg}::$func";
88319179998c5d8d19943d09450931b88694be58takashi } else {
88319179998c5d8d19943d09450931b88694be58takashi $closure_func = substr $func, $index + 2;
88319179998c5d8d19943d09450931b88694be58takashi croak "autouse into different package attempted"
88319179998c5d8d19943d09450931b88694be58takashi unless substr($func, 0, $index) eq $module;
88319179998c5d8d19943d09450931b88694be58takashi }
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi my $load_sub = sub {
88319179998c5d8d19943d09450931b88694be58takashi unless ($INC{$pm}) {
88319179998c5d8d19943d09450931b88694be58takashi eval {require $pm};
88319179998c5d8d19943d09450931b88694be58takashi die if $@;
88319179998c5d8d19943d09450931b88694be58takashi vet_import $module;
88319179998c5d8d19943d09450931b88694be58takashi }
88319179998c5d8d19943d09450931b88694be58takashi no warnings 'redefine';
88319179998c5d8d19943d09450931b88694be58takashi *$closure_import_func = \&{"${module}::$closure_func"};
88319179998c5d8d19943d09450931b88694be58takashi print "autousing $module; "
88319179998c5d8d19943d09450931b88694be58takashi ."imported $closure_func as $closure_import_func\n"
88319179998c5d8d19943d09450931b88694be58takashi if $autouse::DEBUG;
88319179998c5d8d19943d09450931b88694be58takashi goto &$closure_import_func;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf };
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi if (defined $proto) {
88319179998c5d8d19943d09450931b88694be58takashi *$closure_import_func = eval "sub ($proto) { &\$load_sub }";
88319179998c5d8d19943d09450931b88694be58takashi } else {
88319179998c5d8d19943d09450931b88694be58takashi *$closure_import_func = $load_sub;
88319179998c5d8d19943d09450931b88694be58takashi }
88319179998c5d8d19943d09450931b88694be58takashi }
88319179998c5d8d19943d09450931b88694be58takashi}
88319179998c5d8d19943d09450931b88694be58takashi
bd43fc31993cfc191e744a9490481f4294894099covenersub vet_import ($) {
bd43fc31993cfc191e744a9490481f4294894099covener my $module = shift;
bd43fc31993cfc191e744a9490481f4294894099covener if (my $import = $module->can('import')) {
bd43fc31993cfc191e744a9490481f4294894099covener croak "autoused module has unique import() method"
bd43fc31993cfc191e744a9490481f4294894099covener unless defined(&Exporter::import)
bd43fc31993cfc191e744a9490481f4294894099covener && $import == \&Exporter::import;
bd43fc31993cfc191e744a9490481f4294894099covener }
88319179998c5d8d19943d09450931b88694be58takashi}
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi1;
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi__END__
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=head1 NAME
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiautouse - postpone load of modules until a function is used
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=head1 SYNOPSIS
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi use autouse 'Carp' => qw(carp croak);
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh carp "this carp was predeclared and autoused ";
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=head1 DESCRIPTION
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiIf the module C<Module> is already loaded, then the declaration
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi use autouse 'Module' => qw(func1 func2($;$));
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiis equivalent to
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi use Module qw(func1 func2);
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiif C<Module> defines func2() with prototype C<($;$)>, and func1() has
88319179998c5d8d19943d09450931b88694be58takashino prototypes. (At least if C<Module> uses C<Exporter>'s C<import>,
88319179998c5d8d19943d09450931b88694be58takashiotherwise it is a fatal error.)
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiIf the module C<Module> is not loaded yet, then the above declaration
88319179998c5d8d19943d09450931b88694be58takashideclares functions func1() and func2() in the current package. When
88319179998c5d8d19943d09450931b88694be58takashithese functions are called, they load the package C<Module> if needed,
88319179998c5d8d19943d09450931b88694be58takashiand substitute themselves with the correct definitions.
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=begin _deprecated
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi use Module qw(Module::func3);
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiwill work and is the equivalent to:
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi use Module qw(func3);
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiIt is not a very useful feature and has been deprecated.
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=end _deprecated
88319179998c5d8d19943d09450931b88694be58takashi
4aa603e6448b99f9371397d439795c91a93637eand
4aa603e6448b99f9371397d439795c91a93637eand=head1 WARNING
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic
88319179998c5d8d19943d09450931b88694be58takashiUsing C<autouse> will move important steps of your program's execution
88319179998c5d8d19943d09450931b88694be58takashifrom compile time to runtime. This can
9a367ec3d570bcbaf8923dad66cb3b1532963964trawick
9a367ec3d570bcbaf8923dad66cb3b1532963964trawick=over 4
ecdc51f25765b81b6d07389c6fa02a9529411f5drbowen
88319179998c5d8d19943d09450931b88694be58takashi=item *
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiBreak the execution of your program if the module you C<autouse>d has
88319179998c5d8d19943d09450931b88694be58takashisome initialization which it expects to be done early.
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=item *
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashihide bugs in your code since important checks (like correctness of
88319179998c5d8d19943d09450931b88694be58takashiprototypes) is moved from compile time to runtime. In particular, if
88319179998c5d8d19943d09450931b88694be58takashithe prototype you specified on C<autouse> line is wrong, you will not
4aa603e6448b99f9371397d439795c91a93637eandfind it out until the corresponding function is executed. This will be
4aa603e6448b99f9371397d439795c91a93637eandvery unfortunate for functions which are not always called (note that
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalicfor such functions C<autouse>ing gives biggest win, for a workaround
88319179998c5d8d19943d09450931b88694be58takashisee below).
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=back
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiTo alleviate the second problem (partially) it is advised to write
4aa603e6448b99f9371397d439795c91a93637eandyour scripts like this:
4aa603e6448b99f9371397d439795c91a93637eand
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic use Module;
88319179998c5d8d19943d09450931b88694be58takashi use autouse Module => qw(carp($) croak(&$));
88319179998c5d8d19943d09450931b88694be58takashi carp "this carp was predeclared and autoused ";
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiThe first line ensures that the errors in your argument specification
88319179998c5d8d19943d09450931b88694be58takashiare found early. When you ship your application you should comment
88319179998c5d8d19943d09450931b88694be58takashiout the first line, since it makes the second one useless.
4aa603e6448b99f9371397d439795c91a93637eand
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic=head1 AUTHOR
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiIlya Zakharevich (ilya@math.ohio-state.edu)
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashi=head1 SEE ALSO
88319179998c5d8d19943d09450931b88694be58takashi
88319179998c5d8d19943d09450931b88694be58takashiperl(1).
4aa603e6448b99f9371397d439795c91a93637eand
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic=cut
88319179998c5d8d19943d09450931b88694be58takashi