3 #use strict; # debugging only
4 use 5.003_90; # ->can, for my $var
6 $autouse::VERSION = '1.00';
8 my $DEBUG = $ENV{AUTOUSE_DEBUG};
21 (my $pm = $module) =~ s{::}{/}g;
23 if (exists $INC{$pm}) {
25 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
26 # $Exporter::Verbose = 1;
27 return $module->import(map { (my $f = $_) =~ s/\(.*?\)$// } @_);
30 # It is not loaded: need to do real work.
31 my $callpkg = caller(0);
32 print "autouse called from $callpkg\n" if $DEBUG;
37 $proto = $1 if (my $func = $f) =~ s/\((.*)\)$//;
39 my $closure_import_func = $func; # Full name
40 my $closure_func = $func; # Name inside package
41 my $index = index($func, '::');
43 $closure_import_func = "${callpkg}::$func";
45 $closure_func = substr $func, $index + 2;
46 croak "autouse into different package attempted"
47 unless substr($func, 0, $index) eq $module;
56 *$closure_import_func = \&{"${module}::$closure_func"};
57 print "autousing $module; "
58 ."imported $closure_func as $closure_import_func\n"
60 goto &$closure_import_func;
64 *$closure_import_func = eval "sub ($proto) { &\$load_sub }";
66 *$closure_import_func = $load_sub;
73 if (my $import = $module->can('import')) {
74 croak "autoused module has unique import() method"
75 unless defined(\&Exporter::import)
76 && $import == \&Exporter::import;
86 autouse - postpone load of modules until a function is used
90 use autouse 'Carp' => qw(carp croak);
91 carp "this carp was predeclared and autoused ";
95 If the module C<Module> is already loaded, then the declaration
97 use autouse 'Module' => qw(func1 func2($;$) Module::func3);
101 use Module qw(func1 func2);
103 if C<Module> defines func2() with prototype C<($;$)>, and func1() and
104 func3() have no prototypes. (At least if C<Module> uses C<Exporter>'s
105 C<import>, otherwise it is a fatal error.)
107 If the module C<Module> is not loaded yet, then the above declaration
108 declares functions func1() and func2() in the current package, and
109 declares a function Module::func3(). When these functions are called,
110 they load the package C<Module> if needed, and substitute themselves
111 with the correct definitions.
115 Using C<autouse> will move important steps of your program's execution
116 from compile time to runtime. This can
122 Break the execution of your program if the module you C<autouse>d has
123 some initialization which it expects to be done early.
127 hide bugs in your code since important checks (like correctness of
128 prototypes) is moved from compile time to runtime. In particular, if
129 the prototype you specified on C<autouse> line is wrong, you will not
130 find it out until the corresponding function is executed. This will be
131 very unfortunate for functions which are not always called (note that
132 for such functions C<autouse>ing gives biggest win, for a workaround
137 To alleviate the second problem (partially) it is advised to write
138 your scripts like this:
141 use autouse Module => qw(carp($) croak(&$));
142 carp "this carp was predeclared and autoused ";
144 The first line ensures that the errors in your argument specification
145 are found early. When you ship your application you should comment
146 out the first line, since it makes the second one useless.
150 If Module::func3() is autoused, and the module is loaded between the
151 C<autouse> directive and a call to Module::func3(), warnings about
152 redefinition would appear if warnings are enabled.
154 If Module::func3() is autoused, warnings are disabled when loading the
155 module via autoused functions.
159 Ilya Zakharevich (ilya@math.ohio-state.edu)