2 #use strict; # debugging only
5 $autouse::VERSION = '0.02';
19 if (exists $INC{"$module.pm"}) {
20 unless (exists $INC{"Exporter.pm"}) {
21 croak("use autouse with a module which has its own import()")
22 if $module->can('import');
23 return; # Ignore import
25 croak("use autouse with a module which has its own import()")
26 unless $module->can('import') == \&Exporter::import;
27 local $Exporter::ExportLevel = $Exporter::ExportLevel;
28 $Exporter::ExportLevel++;
29 # $Exporter::Verbose = 1;
33 next unless $f =~ s/\((.*)\)$//;
35 my $sub = index($f, "::") != -1 ? $f : "$module" . "::$f";
36 croak("Prototype mismatch on `$sub' when autousing `$module':\n",
37 "\t`$proto' specified, the real one `",
39 unless prototype($sub) eq $proto;
41 return $module->import(@args);
43 # It is not loaded: need to do real work.
44 my $callpkg = caller(0);
45 print "called from `$callpkg'.\n" if $debug;
50 $proto = $1 if $func =~ s/\((.*)\)$//;
51 my $closure_import_func = $func; # Full name
52 my $closure_func = $func; # Name inside package
53 $index = index($func, '::');
56 $closure_import_func = $callpkg . "::$func";
58 $closure_func = substr $func, $index + 2;
59 croak("Trying to autouse into a different package")
60 unless substr($func, 0, $index) eq $module;
61 $disable_w{$module} = 1;
65 local $^W = exists $disable_w{$module}; # Redefinition
66 eval "require $module";
68 croak("Prototype mismatch on `$closure_import_func' ",
69 "after loading `$module':\n",
70 "\t`$proto' specified when autousing, the real one `",
71 prototype($closure_import_func), "'")
73 and prototype($closure_import_func) ne $proto;
74 local $^W = 0; # Redefinition
75 *$closure_import_func = \&{$module . "::$closure_func"}
76 unless \&$closure_import_func == \&{$module . "::$closure_func"};
78 print "In loader for `$module: $closure_import_func => $closure_func'.\n"
80 goto &$closure_import_func;
83 *$closure_import_func = eval "sub ($proto) {&\$load_sub}";
85 *$closure_import_func = $load_sub;
96 autouse - postpone load of modules until a function is used
100 use autouse 'Carp' => qw(carp croak);
101 carp "this carp was predeclared and autoused ";
106 If the module C<Module> is already loaded, then the declaration
108 use autouse 'Module' => qw(func1 func2($;$) Module::func3);
112 use Module qw(func1 func2);
114 if C<Module> defines func2() with prototype C<($;$)>, and func1() and
115 func3() have no prototypes. (At least if C<Module> uses C<Exporter>'s
116 C<import>, otherwise it is a fatal error.)
118 If the module C<Module> is not loaded yet, then the above declaration
119 declares functions func1() and func2() in the current package, and
120 declares a function Module::func3(). When these functions are called,
121 they load the package C<Module> if needed, and substitute themselves
122 with the correct definitions.
126 Using C<autouse> will move important steps of your program's execution
127 from compile time to runtime. This can
133 Break the execution of your program if the module you C<autouse>d has
134 some initialization which it expects to be done early.
138 hide bugs in your code since important checks (like correctness of
139 prototypes) is moved from compile time to runtime. In particular, if
140 the prototype you specified on C<autouse> line is wrong, you will not
141 find it out until the corresponding function is executed. This will be
142 very unfortunate for functions which are not always called (note that
143 for such functions C<autouse>ing gives biggest win, for a workaround
148 To alleviate the second problem (partially) it is advised to write
149 your scripts like this:
152 use autouse Module => qw(carp($) croak(&$));
153 carp "this carp was predeclared and autoused ";
155 The first line ensures that the errors in your argument specification
156 are found early. When you ship your application you should comment
157 out the first line, since it makes the second one useless.
161 If Module::func3() is autoused, and the module is loaded between the
162 C<autouse> directive and a call to Module::func3(), warnings about
163 redefinition would appear if warnings are enabled.
165 If Module::func3() is autoused, warnings are disabled when loading the
166 module via autoused functions.
170 Ilya Zakharevich (ilya@math.ohio-state.edu)