autouse.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
88319179998c5d8d19943d09450931b88694be58takashi#use strict; # debugging only
2e545ce2450a9953665f701bb05350f0d3f26275ndsub croak {
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen Carp::croak(@_);
88319179998c5d8d19943d09450931b88694be58takashisub import {
3f08db06526d6901aa08c110b5bc7dde6bc39905nd croak "usage: use $class MODULE [,SUBS...]" unless @_;
88319179998c5d8d19943d09450931b88694be58takashi my $module = shift;
88319179998c5d8d19943d09450931b88694be58takashi local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
88319179998c5d8d19943d09450931b88694be58takashi # $Exporter::Verbose = 1;
88319179998c5d8d19943d09450931b88694be58takashi return $module->import(map { (my $f = $_) =~ s/\(.*?\)$//; $f } @_);
88319179998c5d8d19943d09450931b88694be58takashi # It is not loaded: need to do real work.
88319179998c5d8d19943d09450931b88694be58takashi print "autouse called from $callpkg\n" if $autouse::DEBUG;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf for my $f (@_) {
88319179998c5d8d19943d09450931b88694be58takashi croak "autouse into different package attempted"
88319179998c5d8d19943d09450931b88694be58takashi eval {require $pm};
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 (defined $proto) {
88319179998c5d8d19943d09450931b88694be58takashi *$closure_import_func = eval "sub ($proto) { &\$load_sub }";
bd43fc31993cfc191e744a9490481f4294894099covener my $module = shift;
bd43fc31993cfc191e744a9490481f4294894099covener croak "autoused module has unique import() method"
bd43fc31993cfc191e744a9490481f4294894099covener unless defined(&Exporter::import)
88319179998c5d8d19943d09450931b88694be58takashiautouse - postpone load of modules until a function is used
88319179998c5d8d19943d09450931b88694be58takashi=head1 SYNOPSIS
88319179998c5d8d19943d09450931b88694be58takashi use autouse 'Carp' => qw(carp croak);
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh carp "this carp was predeclared and autoused ";
88319179998c5d8d19943d09450931b88694be58takashi=head1 DESCRIPTION
88319179998c5d8d19943d09450931b88694be58takashiIf the module C<Module> is already loaded, then the declaration
88319179998c5d8d19943d09450931b88694be58takashi use autouse 'Module' => qw(func1 func2($;$));
88319179998c5d8d19943d09450931b88694be58takashiis equivalent to
88319179998c5d8d19943d09450931b88694be58takashi use Module qw(func1 func2);
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.)
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=begin _deprecated
88319179998c5d8d19943d09450931b88694be58takashi use Module qw(Module::func3);
88319179998c5d8d19943d09450931b88694be58takashiwill work and is the equivalent to:
88319179998c5d8d19943d09450931b88694be58takashi use Module qw(func3);
88319179998c5d8d19943d09450931b88694be58takashiIt is not a very useful feature and has been deprecated.
88319179998c5d8d19943d09450931b88694be58takashi=end _deprecated
4aa603e6448b99f9371397d439795c91a93637eand=head1 WARNING
88319179998c5d8d19943d09450931b88694be58takashiUsing C<autouse> will move important steps of your program's execution
88319179998c5d8d19943d09450931b88694be58takashifrom compile time to runtime. This can
88319179998c5d8d19943d09450931b88694be58takashiBreak the execution of your program if the module you C<autouse>d has
88319179998c5d8d19943d09450931b88694be58takashisome initialization which it expects to be done early.
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
88319179998c5d8d19943d09450931b88694be58takashiTo alleviate the second problem (partially) it is advised to write
4aa603e6448b99f9371397d439795c91a93637eandyour scripts like this:
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic use Module;
88319179998c5d8d19943d09450931b88694be58takashi use autouse Module => qw(carp($) croak(&$));
88319179998c5d8d19943d09450931b88694be58takashi carp "this carp was predeclared and autoused ";
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.
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic=head1 AUTHOR
88319179998c5d8d19943d09450931b88694be58takashiIlya Zakharevich (ilya@math.ohio-state.edu)
88319179998c5d8d19943d09450931b88694be58takashi=head1 SEE ALSO