File Coverage

blib/lib/UNIVERSAL/require.pm
Criterion Covered Total %
statement 39 39 100.0
branch 16 18 88.8
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package UNIVERSAL::require;
2             $UNIVERSAL::require::VERSION = '0.19';
3              
4             # We do this because UNIVERSAL.pm uses CORE::require(). We're going
5             # to put our own require() into UNIVERSAL and that makes an ambiguity.
6             # So we load it up beforehand to avoid that.
7 3     3   72050 BEGIN { require UNIVERSAL }
8              
9             package UNIVERSAL;
10              
11 3     3   188 use 5.006;
  3         18  
12 3     3   15 use strict;
  3         6  
  3         63  
13 3     3   13 use warnings;
  3         8  
  3         103  
14 3     3   16 use Carp;
  3         6  
  3         1447  
15              
16             # regexp for valid module name. Lifted from Module::Runtime
17             my $module_name_rx = qr/[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/;
18              
19             our $Level = 0;
20              
21             =pod
22              
23             =head1 NAME
24              
25             UNIVERSAL::require - require() modules from a variable [deprecated]
26              
27             =head1 SYNOPSIS
28              
29             # This only needs to be said once in your program.
30             require UNIVERSAL::require;
31              
32             # Same as "require Some::Module"
33             my $module = 'Some::Module';
34             $module->require or die $@;
35              
36             # Same as "use Some::Module"
37             BEGIN { $module->use or die $@ }
38              
39              
40             =head1 DESCRIPTION
41              
42             Before using this module, you should look at the alternatives,
43             some of which are listed in SEE ALSO below.
44              
45             This module provides a safe mechanism for loading a module at runtime,
46             when you have the name of the module in a variable.
47              
48             If you've ever had to do this...
49              
50             eval "require $module";
51              
52             to get around the bareword caveats on require(), this module is for
53             you. It creates a universal require() class method that will work
54             with every Perl module and its secure. So instead of doing some
55             arcane eval() work, you can do this:
56              
57             $module->require;
58              
59             It doesn't save you much typing, but it'll make a lot more sense to
60             someone who's not a ninth level Perl acolyte.
61              
62             =head1 Methods
63              
64             =head3 require
65              
66             my $return_val = $module->require or die $@;
67             my $return_val = $module->require($version) or die $@;
68              
69             This works exactly like Perl's require, except without the bareword
70             restriction, and it doesn't die. Since require() is placed in the
71             UNIVERSAL namespace, it will work on B module. You just have to
72             use UNIVERSAL::require somewhere in your code.
73              
74             Should the module require fail, or not be a high enough $version, it
75             will simply return false and B. The error will be in
76             $@ as well as $UNIVERSAL::require::ERROR.
77              
78             $module->require or die $@;
79              
80             =cut
81              
82             sub require {
83 10     10 0 3003 my($module, $want_version) = @_;
84              
85 10         22 $UNIVERSAL::require::ERROR = '';
86              
87 10 50       32 croak("UNIVERSAL::require() can only be run as a class method")
88             if ref $module;
89              
90 10 100       338 croak("invalid module name '$module'") if $module !~ /\A$module_name_rx\z/;
91              
92 9 50       32 croak("UNIVERSAL::require() takes no or one arguments") if @_ > 2;
93              
94 9         62 my($call_package, $call_file, $call_line) = caller($Level);
95              
96             # Load the module.
97 9         29 my $file = $module . '.pm';
98 9         22 $file =~ s{::}{/}g;
99              
100             # For performance reasons, check if its already been loaded. This makes
101             # things about 4 times faster.
102             # We use the eval { } to make sure $@ is not set. See RT #44444 for details
103 9 100       30 return eval { 1 } if $INC{$file};
  3         20  
104              
105 6         191 my $return = eval qq{
106             #line $call_line "$call_file"
107             CORE::require(\$file);
108             };
109              
110             # Check for module load failure.
111 6 100       1720 if( !$return ) {
112 2         7 $UNIVERSAL::require::ERROR = $@;
113 2         12 return $return;
114             }
115              
116             # Module version check.
117 4 100       15 if( @_ == 2 ) {
118             eval qq{
119             #line $call_line "$call_file"
120             \$module->VERSION($want_version);
121             1;
122 2 100       77 } or do {
123 1         28 $UNIVERSAL::require::ERROR = $@;
124 1         12 return 0;
125             };
126             }
127 3         45 return $return;
128             }
129              
130              
131             =head3 use
132              
133             my $require_return = $module->use or die $@;
134             my $require_return = $module->use(@imports) or die $@;
135              
136             Like C, this allows you to C a $module without
137             having to eval to work around the bareword requirement. It returns the
138             same as require.
139              
140             Should either the require or the import fail it will return false. The
141             error will be in $@.
142              
143             If possible, call this inside a BEGIN block to emulate a normal C
144             as closely as possible.
145              
146             BEGIN { $module->use }
147              
148             =cut
149              
150             sub use {
151 4     4 0 1560 my($module, @imports) = @_;
152              
153 4         8 local $Level = 1;
154 4 100       14 my $return = $module->require or return 0;
155              
156 3         11 my($call_package, $call_file, $call_line) = caller;
157              
158             eval qq{
159             package $call_package;
160             #line $call_line "$call_file"
161             \$module->import(\@imports);
162             1;
163 3 100       167 } or do {
164 1         107 $UNIVERSAL::require::ERROR = $@;
165 1         9 return 0;
166             };
167              
168 2         93 return $return;
169             }
170              
171              
172             =head1 SECURITY NOTES
173              
174             UNIVERSAL::require makes use of C. In previous versions
175             of UNIVERSAL::require it was discovered that one could craft a class
176             name which would result in code being executed. This hole has been
177             closed. The only variables now exposed to C are the
178             caller's package, filename and line which are not tainted.
179              
180             UNIVERSAL::require is taint clean.
181              
182              
183             =head1 COPYRIGHT
184              
185             Copyright 2001, 2005 by Michael G Schwern Eschwern@pobox.comE.
186              
187             This program is free software; you can redistribute it and/or
188             modify it under the same terms as Perl itself.
189              
190             See F
191              
192              
193             =head1 AUTHOR
194              
195             Michael G Schwern
196              
197             Now maintained by Neil Bowers (NEILB).
198              
199             =head1 SEE ALSO
200              
201             L provides functions for loading code,
202             and importing functions.
203             It's actively maintained.
204              
205             L provides a number of usesful functions
206             for require'ing and use'ing modules,
207             and associated operations.
208              
209             L is a class loader and plugin framework.
210             L is a stand-alone module that was inspired
211             by C.
212              
213             There are many other modules that may be of interest on CPAN.
214             An old review of some of them can be read at
215             L.
216              
217             L.
218              
219             =cut
220              
221              
222             1;