File Coverage

blib/lib/UNIVERSAL/require.pm
Criterion Covered Total %
statement 40 40 100.0
branch 16 18 88.8
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 63 67 94.0


line stmt bran cond sub pod time code
1             package UNIVERSAL::require;
2             $UNIVERSAL::require::VERSION = '0.18';
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   29544 BEGIN { require UNIVERSAL }
8              
9             package UNIVERSAL;
10              
11 3     3   206 use 5.006;
  3         16  
  3         109  
12 3     3   15 use strict;
  3         5  
  3         127  
13 3     3   14 use warnings;
  3         4  
  3         104  
14 3     3   14 use Carp;
  3         3  
  3         1511  
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
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             If you've ever had to do this...
43              
44             eval "require $module";
45              
46             to get around the bareword caveats on require(), this module is for
47             you. It creates a universal require() class method that will work
48             with every Perl module and its secure. So instead of doing some
49             arcane eval() work, you can do this:
50              
51             $module->require;
52              
53             It doesn't save you much typing, but it'll make a lot more sense to
54             someone who's not a ninth level Perl acolyte.
55              
56             =head1 Methods
57              
58             =head3 require
59              
60             my $return_val = $module->require or die $@;
61             my $return_val = $module->require($version) or die $@;
62              
63             This works exactly like Perl's require, except without the bareword
64             restriction, and it doesn't die. Since require() is placed in the
65             UNIVERSAL namespace, it will work on B module. You just have to
66             use UNIVERSAL::require somewhere in your code.
67              
68             Should the module require fail, or not be a high enough $version, it
69             will simply return false and B. The error will be in
70             $@ as well as $UNIVERSAL::require::ERROR.
71              
72             $module->require or die $@;
73              
74             =cut
75              
76             sub require {
77 10     10 0 3103 my($module, $want_version) = @_;
78              
79 10         16 $UNIVERSAL::require::ERROR = '';
80              
81 10 50       26 croak("UNIVERSAL::require() can only be run as a class method")
82             if ref $module;
83              
84 10 100       374 croak("invalid module name '$module'") if $module !~ /\A$module_name_rx\z/;
85              
86 9 50       30 croak("UNIVERSAL::require() takes no or one arguments") if @_ > 2;
87              
88 9         55 my($call_package, $call_file, $call_line) = caller($Level);
89              
90             # Load the module.
91 9         23 my $file = $module . '.pm';
92 9         16 $file =~ s{::}{/}g;
93              
94             # For performance reasons, check if its already been loaded. This makes
95             # things about 4 times faster.
96             # We use the eval { } to make sure $@ is not set. See RT #44444 for details
97 9 100       23 return eval { 1 } if $INC{$file};
  3         11  
98              
99 6         172 my $return = eval qq{
100             #line $call_line "$call_file"
101             CORE::require(\$file);
102             };
103              
104             # Check for module load failure.
105 6 100       1383 if( !$return ) {
106 2         4 $UNIVERSAL::require::ERROR = $@;
107 2         11 return $return;
108             }
109              
110             # Module version check.
111 4 100       14 if( @_ == 2 ) {
112             eval qq{
113             #line $call_line "$call_file"
114             \$module->VERSION($want_version);
115             1;
116 2 100       77 } or do {
117 1         33 $UNIVERSAL::require::ERROR = $@;
118 1         6 return 0;
119             };
120             }
121 3         40 return $return;
122             }
123              
124              
125             =head3 use
126              
127             my $require_return = $module->use or die $@;
128             my $require_return = $module->use(@imports) or die $@;
129              
130             Like C, this allows you to C a $module without
131             having to eval to work around the bareword requirement. It returns the
132             same as require.
133              
134             Should either the require or the import fail it will return false. The
135             error will be in $@.
136              
137             If possible, call this inside a BEGIN block to emulate a normal C
138             as closely as possible.
139              
140             BEGIN { $module->use }
141              
142             =cut
143              
144             sub use {
145 4     4 0 1590 my($module, @imports) = @_;
146              
147 4         19 local $Level = 1;
148 4 100       14 my $return = $module->require or return 0;
149              
150 3         6 my($call_package, $call_file, $call_line) = caller;
151              
152             eval qq{
153             package $call_package;
154             #line $call_line "$call_file"
155             \$module->import(\@imports);
156             1;
157 3 100       117 } or do {
158 1         134 $UNIVERSAL::require::ERROR = $@;
159 1         6 return 0;
160             };
161              
162 2         82 return $return;
163             }
164              
165              
166             =head1 SECURITY NOTES
167              
168             UNIVERSAL::require makes use of C. In previous versions
169             of UNIVERSAL::require it was discovered that one could craft a class
170             name which would result in code being executed. This hole has been
171             closed. The only variables now exposed to C are the
172             caller's package, filename and line which are not tainted.
173              
174             UNIVERSAL::require is taint clean.
175              
176              
177             =head1 COPYRIGHT
178              
179             Copyright 2001, 2005 by Michael G Schwern Eschwern@pobox.comE.
180              
181             This program is free software; you can redistribute it and/or
182             modify it under the same terms as Perl itself.
183              
184             See F
185              
186              
187             =head1 AUTHOR
188              
189             Michael G Schwern
190              
191             Now maintained by Neil Bowers (NEILB).
192              
193             =head1 SEE ALSO
194              
195             L, L, L
196              
197             =cut
198              
199              
200             1;