3 #use strict; # debugging only
4 use 5.003_90; # ->can, for my $var
6 $autouse::VERSION = '1.04';
18 my $class = @_ ? shift : 'autouse';
19 croak "usage: use $class MODULE [,SUBS...]" unless @_;
22 (my $pm = $module) =~ s{::}{/}g;
24 if (exists $INC{$pm}) {
26 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
27 # $Exporter::Verbose = 1;
28 return $module->import(map { (my $f = $_) =~ s/\(.*?\)$//; $f } @_);
31 # It is not loaded: need to do real work.
32 my $callpkg = caller(0);
33 print "autouse called from $callpkg\n" if $autouse::DEBUG;
38 $proto = $1 if (my $func = $f) =~ s/\((.*)\)$//;
40 my $closure_import_func = $func; # Full name
41 my $closure_func = $func; # Name inside package
42 my $index = rindex($func, '::');
44 $closure_import_func = "${callpkg}::$func";
46 $closure_func = substr $func, $index + 2;
47 croak "autouse into different package attempted"
48 unless substr($func, 0, $index) eq $module;
57 no warnings 'redefine';
58 *$closure_import_func = \&{"${module}::$closure_func"};
59 print "autousing $module; "
60 ."imported $closure_func as $closure_import_func\n"
62 goto &$closure_import_func;
66 *$closure_import_func = eval "sub ($proto) { &\$load_sub }";
68 *$closure_import_func = $load_sub;
75 if (my $import = $module->can('import')) {
76 croak "autoused module $module has unique import() method"
77 unless defined(&Exporter::import)
78 && ($import == \&Exporter::import ||
79 $import == \&UNIVERSAL::import)
89 autouse - postpone load of modules until a function is used
93 use autouse 'Carp' => qw(carp croak);
94 carp "this carp was predeclared and autoused ";
98 If the module C<Module> is already loaded, then the declaration
100 use autouse 'Module' => qw(func1 func2($;$));
104 use Module qw(func1 func2);
106 if C<Module> defines func2() with prototype C<($;$)>, and func1() has
107 no prototypes. (At least if C<Module> uses C<Exporter>'s C<import>,
108 otherwise it is a fatal error.)
110 If the module C<Module> is not loaded yet, then the above declaration
111 declares functions func1() and func2() in the current package. When
112 these functions are called, they load the package C<Module> if needed,
113 and substitute themselves with the correct definitions.
117 use Module qw(Module::func3);
119 will work and is the equivalent to:
121 use Module qw(func3);
123 It is not a very useful feature and has been deprecated.
130 Using C<autouse> will move important steps of your program's execution
131 from compile time to runtime. This can
137 Break the execution of your program if the module you C<autouse>d has
138 some initialization which it expects to be done early.
142 hide bugs in your code since important checks (like correctness of
143 prototypes) is moved from compile time to runtime. In particular, if
144 the prototype you specified on C<autouse> line is wrong, you will not
145 find it out until the corresponding function is executed. This will be
146 very unfortunate for functions which are not always called (note that
147 for such functions C<autouse>ing gives biggest win, for a workaround
152 To alleviate the second problem (partially) it is advised to write
153 your scripts like this:
156 use autouse Module => qw(carp($) croak(&$));
157 carp "this carp was predeclared and autoused ";
159 The first line ensures that the errors in your argument specification
160 are found early. When you ship your application you should comment
161 out the first line, since it makes the second one useless.
165 Ilya Zakharevich (ilya@math.ohio-state.edu)