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