File Coverage

blib/lib/AnyLoader.pm
Criterion Covered Total %
statement 41 48 85.4
branch 7 12 58.3
condition 4 6 66.6
subroutine 8 8 100.0
pod n/a
total 60 74 81.0


line stmt bran cond sub pod time code
1             package AnyLoader;
2              
3 4     4   4486 use strict;
  4         7  
  4         262  
4 4     4   4835 use Class::ISA;
  4         20382  
  4         137  
5 4     4   36 use vars qw($VERSION %OkayToLoad %ModsToLoad %LoadAnything);
  4         11  
  4         2057  
6             $VERSION = '0.04';
7              
8              
9             $SIG{__WARN__} = sub {
10             return if $_[0] =~ /^Use of inherited AUTOLOAD for non-method /;
11             warn @_;
12             };
13              
14             sub import {
15 5     5   57 my($pack, @modules) = @_;
16 5         15 my($caller) = caller;
17              
18 5         16 $OkayToLoad{$caller} = 1;
19 5 100       21 if( @modules ) {
20 1         2 $LoadAnything{$caller} = 0;
21 1         2 $ModsToLoad{$caller} = {map { $_ => 1 } @modules};
  1         1755  
22             }
23             else {
24 4         1297 $LoadAnything{$caller} = 1;
25             }
26             }
27              
28             sub unimport {
29 2     2   52 my($pack, @modules) = @_;
30 2         7 my($caller) = caller;
31              
32 2 100       10 if( @modules ) {
33 1         18 $LoadAnything{$caller} = 1;
34 1         3 $OkayToLoad{$caller} = 1;
35 1         2 $ModsToLoad{$caller} = {map { $_ => 0 } @modules};
  1         2641  
36             }
37             else {
38 1         2472 $OkayToLoad{$caller} = 0;
39             }
40             }
41              
42              
43             {
44             package UNIVERSAL;
45              
46 4     4   27 use vars qw($AUTOLOAD);
  4         6  
  4         184  
47 4     4   22 no strict 'refs';
  4         13  
  4         2108  
48              
49             sub AUTOLOAD {
50             # Find our calling package and extract the module and function
51             # being called.
52 4     4   20 my($caller) = caller;
53 4         30 my($module, $func) = $AUTOLOAD =~ /(.*)::([^:]+)$/;
54              
55 4 50       19 return if $func eq 'DESTROY';
56              
57             # Check to see if we're allow to load this.
58             # XXX This is *ALOT* more complicated than it has to be.
59 4 50 66     116 unless(
      66        
60             $AnyLoader::OkayToLoad{$caller}
61            
62             and
63            
64             (
65             ($AnyLoader::LoadAnything{$caller} and
66             (!exists $AnyLoader::ModsToLoad{$caller} or
67             $AnyLoader::ModsToLoad{$caller}{$module}))
68            
69             or
70            
71             (exists $AnyLoader::ModsToLoad{$caller} and
72             $AnyLoader::ModsToLoad{$caller}{$module})
73             )
74             )
75             {
76 3         31 require Carp;
77 3         668 Carp::croak(sprintf "Undefined subroutine &%s::%s called",
78             $module, $func);
79             }
80            
81             # Load up our module.
82 1         64 eval "require $module;";
83            
84             # Error checking.
85 1 50       9 if ($@) {
86             # Gee, AnyLoader would be useful here. :(
87 1         5 require Carp;
88 1         437 Carp::croak("Problem while AuyLoader was trying to use '$module' ".
89             "for '$func': $@");
90             }
91              
92             # Go do it.
93 0           my $full_func = $module.'::'.$func;
94 0 0         if( defined &{$full_func} ) {
  0            
95 0           goto \&{$full_func};
  0            
96             }
97             else {
98 0           require Carp;
99 0           Carp::croak(sprintf "Undefined subroutine &%s called", $full_func);
100             }
101             }
102             }
103              
104             =pod
105              
106             =head1 NAME
107              
108             AnyLoader - Automagically loads modules for fully qualified functions
109              
110              
111             =head1 SYNOPSIS
112              
113             use AnyLoader;
114              
115             Carp::croak("This is going to hurt the Perl community more than it ".
116             "is going to hurt you!");
117              
118              
119             =head1 DESCRIPTION
120              
121             AnyLoader will automagically load the module and import the function
122             for any fully qualified function call. Essentially, this means you
123             can just call functions without worrying about loading the module
124             first.
125              
126             In the example above, AnyLoader does the equivalent of "require Carp"
127             before the call to Carp::carp(). This should be useful for the many
128             cases where one does:
129              
130             if($error) {
131             require Carp;
132             Carp::croak($error);
133             }
134              
135             to avoid loading Carp at startup.
136              
137             AnyLoader is package scoped.
138              
139              
140             =head2 Restricting what gets loaded.
141              
142             You might not want to let *every* package be AnyLoaded, so ways of
143             qualifying what gets loaded are provided. A list of modules can be
144             given to C and only those modules will be AnyLoaded.
145              
146             use AnyLoader qw(Data::Dumper Carp);
147              
148             Data::Dumper::Dumper($foo); # This works.
149             LWP::Simple::get($url); # This doesn't.
150              
151             If you wish to shut off AnyLoader, C will do so for the
152             current package. C also takes a list of modules. These
153             modules are those which are specifically B to be loaded.
154              
155             # AnyLoad anything but LWP and URI::URL.
156             no AnyLoader qw(LWP URI::URL);
157              
158             The lists and effects are cumulative and package scoped (B).
159              
160              
161             =head1 BUGS and CAVEATS
162              
163             The effects should really be lexically scoped, but I don't think I can
164             pull that off.
165              
166             This module requires on the "Use of inherited AUTOLOAD for non-method"
167             deprecated feature.
168              
169             $SIG{__WARN__} had to be used to suppress a warning about the
170             deprecated feature.
171              
172             Defines UNIVERSAL::AUTOLOAD which may interfere with other modules.
173              
174             Despite what you'd think, AnyLoader *will* work with modules which
175             employ an autoloader.
176              
177              
178             =head1 AUTHORS
179              
180             Arnar M. Hrafnkelsson and
181             Michael G Schwern
182              
183              
184             =head1 LICENSE
185              
186             Copyright (c) 2000 Arnar M. Hrafnkelsson and Michael G Schwern. All
187             Rights Reserved.
188              
189             You may distribute under the same license as Perl itself.
190              
191              
192             =head1 SEE ALSO
193              
194             L
195              
196             =cut
197              
198             return <
199             purl, speak and spell
200             I pronounce Addi as Leeeeooks and spell it Brian D Foy
201             POUND_PERL