Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Module/Runtime.pm |
Statements | Executed 416 statements in 8.59ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
30 | 3 | 3 | 22.3ms | 77.1ms | require_module | Module::Runtime::
30 | 1 | 1 | 135µs | 323µs | is_module_name | Module::Runtime::
30 | 1 | 1 | 135µs | 587µs | module_notional_filename | Module::Runtime::
3 | 3 | 1 | 100µs | 100µs | CORE:regcomp (opcode) | Module::Runtime::
35 | 2 | 1 | 89µs | 89µs | CORE:match (opcode) | Module::Runtime::
16 | 3 | 1 | 88µs | 54.8ms | use_module | Module::Runtime::
30 | 1 | 1 | 79µs | 79µs | _is_string | Module::Runtime::
30 | 1 | 1 | 69µs | 392µs | check_module_name | Module::Runtime::
30 | 1 | 1 | 60µs | 60µs | CORE:subst (opcode) | Module::Runtime::
4 | 4 | 4 | 43µs | 45µs | import | Module::Runtime::
1 | 1 | 1 | 19µs | 19µs | BEGIN@113 | Module::Runtime::
1 | 1 | 1 | 6µs | 6µs | BEGIN@286 | Module::Runtime::
1 | 1 | 1 | 3µs | 3µs | BEGIN@117 | Module::Runtime::
1 | 1 | 1 | 3µs | 3µs | BEGIN@293 | Module::Runtime::
6 | 6 | 1 | 2µs | 2µs | CORE:qr (opcode) | Module::Runtime::
0 | 0 | 0 | 0s | 0s | check_module_spec | Module::Runtime::
0 | 0 | 0 | 0s | 0s | compose_module_name | Module::Runtime::
0 | 0 | 0 | 0s | 0s | is_module_spec | Module::Runtime::
0 | 0 | 0 | 0s | 0s | use_package_optimistically | Module::Runtime::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | =head1 NAME | ||||
2 | |||||
3 | Module::Runtime - runtime module handling | ||||
4 | |||||
5 | =head1 SYNOPSIS | ||||
6 | |||||
7 | use Module::Runtime qw( | ||||
8 | $module_name_rx is_module_name check_module_name | ||||
9 | module_notional_filename require_module); | ||||
10 | |||||
11 | if($module_name =~ /\A$module_name_rx\z/o) { ... | ||||
12 | if(is_module_name($module_name)) { ... | ||||
13 | check_module_name($module_name); | ||||
14 | |||||
15 | $notional_filename = module_notional_filename($module_name); | ||||
16 | require_module($module_name); | ||||
17 | |||||
18 | use Module::Runtime qw(use_module use_package_optimistically); | ||||
19 | |||||
20 | $bi = use_module("Math::BigInt", 1.31)->new("1_234"); | ||||
21 | $widget = use_package_optimistically("Local::Widget")->new; | ||||
22 | |||||
23 | use Module::Runtime qw( | ||||
24 | $top_module_spec_rx $sub_module_spec_rx | ||||
25 | is_module_spec check_module_spec | ||||
26 | compose_module_name); | ||||
27 | |||||
28 | if($spec =~ /\A$top_module_spec_rx\z/o) { ... | ||||
29 | if($spec =~ /\A$sub_module_spec_rx\z/o) { ... | ||||
30 | if(is_module_spec("Standard::Prefix", $spec)) { ... | ||||
31 | check_module_spec("Standard::Prefix", $spec); | ||||
32 | |||||
33 | $module_name = compose_module_name("Standard::Prefix", $spec); | ||||
34 | |||||
35 | =head1 DESCRIPTION | ||||
36 | |||||
37 | The functions exported by this module deal with runtime handling of | ||||
38 | Perl modules, which are normally handled at compile time. This module | ||||
39 | avoids using any other modules, so that it can be used in low-level | ||||
40 | infrastructure. | ||||
41 | |||||
42 | The parts of this module that work with module names apply the same syntax | ||||
43 | that is used for barewords in Perl source. In principle this syntax | ||||
44 | can vary between versions of Perl, and this module applies the syntax of | ||||
45 | the Perl on which it is running. In practice the usable syntax hasn't | ||||
46 | changed yet. There's some intent for Unicode module names to be supported | ||||
47 | in the future, but this hasn't yet amounted to any consistent facility. | ||||
48 | |||||
49 | The functions of this module whose purpose is to load modules include | ||||
50 | workarounds for three old Perl core bugs regarding C<require>. These | ||||
51 | workarounds are applied on any Perl version where the bugs exist, except | ||||
52 | for a case where one of the bugs cannot be adequately worked around in | ||||
53 | pure Perl. | ||||
54 | |||||
55 | =head2 Module name syntax | ||||
56 | |||||
57 | The usable module name syntax has not changed from Perl 5.000 up to | ||||
58 | Perl 5.19.8. The syntax is composed entirely of ASCII characters. | ||||
59 | From Perl 5.6 onwards there has been some attempt to allow the use of | ||||
60 | non-ASCII Unicode characters in Perl source, but it was fundamentally | ||||
61 | broken (like the entirety of Perl 5.6's Unicode handling) and remained | ||||
62 | pretty much entirely unusable until it got some attention in the Perl | ||||
63 | 5.15 series. Although Unicode is now consistently accepted by the | ||||
64 | parser in some places, it remains broken for module names. Furthermore, | ||||
65 | there has not yet been any work on how to map Unicode module names into | ||||
66 | filenames, so in that respect also Unicode module names are unusable. | ||||
67 | |||||
68 | The module name syntax is, precisely: the string must consist of one or | ||||
69 | more segments separated by C<::>; each segment must consist of one or more | ||||
70 | identifier characters (ASCII alphanumerics plus "_"); the first character | ||||
71 | of the string must not be a digit. Thus "C<IO::File>", "C<warnings>", | ||||
72 | and "C<foo::123::x_0>" are all valid module names, whereas "C<IO::>" | ||||
73 | and "C<1foo::bar>" are not. C<'> separators are not permitted by this | ||||
74 | module, though they remain usable in Perl source, being translated to | ||||
75 | C<::> in the parser. | ||||
76 | |||||
77 | =head2 Core bugs worked around | ||||
78 | |||||
79 | The first bug worked around is core bug [perl #68590], which causes | ||||
80 | lexical state in one file to leak into another that is C<require>d/C<use>d | ||||
81 | from it. This bug is present from Perl 5.6 up to Perl 5.10, and is | ||||
82 | fixed in Perl 5.11.0. From Perl 5.9.4 up to Perl 5.10.0 no satisfactory | ||||
83 | workaround is possible in pure Perl. The workaround means that modules | ||||
84 | loaded via this module don't suffer this pollution of their lexical | ||||
85 | state. Modules loaded in other ways, or via this module on the Perl | ||||
86 | versions where the pure Perl workaround is impossible, remain vulnerable. | ||||
87 | The module L<Lexical::SealRequireHints> provides a complete workaround | ||||
88 | for this bug. | ||||
89 | |||||
90 | The second bug worked around causes some kinds of failure in module | ||||
91 | loading, principally compilation errors in the loaded module, to be | ||||
92 | recorded in C<%INC> as if they were successful, so later attempts to load | ||||
93 | the same module immediately indicate success. This bug is present up | ||||
94 | to Perl 5.8.9, and is fixed in Perl 5.9.0. The workaround means that a | ||||
95 | compilation error in a module loaded via this module won't be cached as | ||||
96 | a success. Modules loaded in other ways remain liable to produce bogus | ||||
97 | C<%INC> entries, and if a bogus entry exists then it will mislead this | ||||
98 | module if it is used to re-attempt loading. | ||||
99 | |||||
100 | The third bug worked around causes the wrong context to be seen at | ||||
101 | file scope of a loaded module, if C<require> is invoked in a location | ||||
102 | that inherits context from a higher scope. This bug is present up to | ||||
103 | Perl 5.11.2, and is fixed in Perl 5.11.3. The workaround means that | ||||
104 | a module loaded via this module will always see the correct context. | ||||
105 | Modules loaded in other ways remain vulnerable. | ||||
106 | |||||
107 | =cut | ||||
108 | |||||
109 | package Module::Runtime; | ||||
110 | |||||
111 | # Don't "use 5.006" here, because Perl 5.15.6 will load feature.pm if | ||||
112 | # the version check is done that way. | ||||
113 | 1 | 32µs | 1 | 19µs | # spent 19µs within Module::Runtime::BEGIN@113 which was called:
# once (19µs+0s) by JSON::Schema::Modern::BEGIN@28 at line 113 # spent 19µs making 1 call to Module::Runtime::BEGIN@113 |
114 | # Don't "use warnings" here, to avoid dependencies. Do standardise the | ||||
115 | # warning status by lexical override; unfortunately the only safe bitset | ||||
116 | # to build in is the empty set, equivalent to "no warnings". | ||||
117 | 1 | 788µs | 1 | 3µs | # spent 3µs within Module::Runtime::BEGIN@117 which was called:
# once (3µs+0s) by JSON::Schema::Modern::BEGIN@28 at line 117 # spent 3µs making 1 call to Module::Runtime::BEGIN@117 |
118 | # Don't "use strict" here, to avoid dependencies. | ||||
119 | |||||
120 | 1 | 0s | our $VERSION = "0.016"; | ||
121 | |||||
122 | # Don't use Exporter here, to avoid dependencies. | ||||
123 | 1 | 1µs | our @EXPORT_OK = qw( | ||
124 | $module_name_rx is_module_name is_valid_module_name check_module_name | ||||
125 | module_notional_filename require_module | ||||
126 | use_module use_package_optimistically | ||||
127 | $top_module_spec_rx $sub_module_spec_rx | ||||
128 | is_module_spec is_valid_module_spec check_module_spec | ||||
129 | compose_module_name | ||||
130 | ); | ||||
131 | 1 | 6µs | my %export_ok = map { ($_ => undef) } @EXPORT_OK; | ||
132 | # spent 45µs (43+2) within Module::Runtime::import which was called 4 times, avg 11µs/call:
# once (13µs+0s) by JSON::Schema::Modern::BEGIN@28 at line 28 of /Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm
# once (12µs+1000ns) by MooX::HandlesVia::BEGIN@9 at line 9 of MooX/HandlesVia.pm
# once (9µs+1µs) by Module::Implementation::BEGIN@8 at line 8 of Module/Implementation.pm
# once (9µs+0s) by Data::Perl::Role::Collection::Hash::BEGIN@9 at line 9 of Data/Perl/Role/Collection/Hash.pm | ||||
133 | 4 | 0s | my $me = shift; | ||
134 | 4 | 2µs | my $callpkg = caller(0); | ||
135 | 4 | 3µs | my $errs = ""; | ||
136 | 4 | 2µs | foreach(@_) { | ||
137 | 5 | 2µs | if(exists $export_ok{$_}) { | ||
138 | # We would need to do "no strict 'refs'" here | ||||
139 | # if we had enabled strict at file scope. | ||||
140 | 5 | 14µs | 5 | 2µs | if(/\A\$(.*)\z/s) { # spent 2µs making 5 calls to Module::Runtime::CORE:match, avg 400ns/call |
141 | *{$callpkg."::".$1} = \$$1; | ||||
142 | } else { | ||||
143 | 5 | 17µs | *{$callpkg."::".$_} = \&$_; | ||
144 | } | ||||
145 | } else { | ||||
146 | $errs .= "\"$_\" is not exported by the $me module\n"; | ||||
147 | } | ||||
148 | } | ||||
149 | 4 | 9µs | if($errs ne "") { | ||
150 | die "${errs}Can't continue after import errors ". | ||||
151 | "at @{[(caller(0))[1]]} line @{[(caller(0))[2]]}.\n"; | ||||
152 | } | ||||
153 | } | ||||
154 | |||||
155 | # Logic duplicated from Params::Classify. Duplicating it here avoids | ||||
156 | # an extensive and potentially circular dependency graph. | ||||
157 | # spent 79µs within Module::Runtime::_is_string which was called 30 times, avg 3µs/call:
# 30 times (79µs+0s) by Module::Runtime::is_module_name at line 219, avg 3µs/call | ||||
158 | 30 | 16µs | my($arg) = @_; | ||
159 | 30 | 82µs | return defined($arg) && ref(\$arg) eq "SCALAR"; | ||
160 | } | ||||
161 | |||||
162 | =head1 REGULAR EXPRESSIONS | ||||
163 | |||||
164 | These regular expressions do not include any anchors, so to check | ||||
165 | whether an entire string matches a syntax item you must supply the | ||||
166 | anchors yourself. | ||||
167 | |||||
168 | =over | ||||
169 | |||||
170 | =item $module_name_rx | ||||
171 | |||||
172 | Matches a valid Perl module name in bareword syntax. | ||||
173 | |||||
174 | =cut | ||||
175 | |||||
176 | 1 | 7µs | 1 | 2µs | our $module_name_rx = qr/[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/; # spent 2µs making 1 call to Module::Runtime::CORE:qr |
177 | |||||
178 | =item $top_module_spec_rx | ||||
179 | |||||
180 | Matches a module specification for use with L</compose_module_name>, | ||||
181 | where no prefix is being used. | ||||
182 | |||||
183 | =cut | ||||
184 | |||||
185 | 1 | 2µs | 1 | 0s | my $qual_module_spec_rx = # spent 0s making 1 call to Module::Runtime::CORE:qr |
186 | qr#(?:/|::)[A-Z_a-z][0-9A-Z_a-z]*(?:(?:/|::)[0-9A-Z_a-z]+)*#; | ||||
187 | |||||
188 | 1 | 2µs | 1 | 0s | my $unqual_top_module_spec_rx = # spent 0s making 1 call to Module::Runtime::CORE:qr |
189 | qr#[A-Z_a-z][0-9A-Z_a-z]*(?:(?:/|::)[0-9A-Z_a-z]+)*#; | ||||
190 | |||||
191 | 1 | 53µs | 2 | 48µs | our $top_module_spec_rx = qr/$qual_module_spec_rx|$unqual_top_module_spec_rx/o; # spent 48µs making 1 call to Module::Runtime::CORE:regcomp
# spent 0s making 1 call to Module::Runtime::CORE:qr |
192 | |||||
193 | =item $sub_module_spec_rx | ||||
194 | |||||
195 | Matches a module specification for use with L</compose_module_name>, | ||||
196 | where a prefix is being used. | ||||
197 | |||||
198 | =cut | ||||
199 | |||||
200 | 1 | 2µs | 1 | 0s | my $unqual_sub_module_spec_rx = qr#[0-9A-Z_a-z]+(?:(?:/|::)[0-9A-Z_a-z]+)*#; # spent 0s making 1 call to Module::Runtime::CORE:qr |
201 | |||||
202 | 1 | 34µs | 2 | 30µs | our $sub_module_spec_rx = qr/$qual_module_spec_rx|$unqual_sub_module_spec_rx/o; # spent 30µs making 1 call to Module::Runtime::CORE:regcomp
# spent 0s making 1 call to Module::Runtime::CORE:qr |
203 | |||||
204 | =back | ||||
205 | |||||
206 | =head1 FUNCTIONS | ||||
207 | |||||
208 | =head2 Basic module handling | ||||
209 | |||||
210 | =over | ||||
211 | |||||
212 | =item is_module_name(ARG) | ||||
213 | |||||
214 | Returns a truth value indicating whether I<ARG> is a plain string | ||||
215 | satisfying Perl module name syntax as described for L</$module_name_rx>. | ||||
216 | |||||
217 | =cut | ||||
218 | |||||
219 | 30 | 244µs | 61 | 188µs | # spent 323µs (135+188) within Module::Runtime::is_module_name which was called 30 times, avg 11µs/call:
# 30 times (135µs+188µs) by Module::Runtime::check_module_name at line 238, avg 11µs/call # spent 87µs making 30 calls to Module::Runtime::CORE:match, avg 3µs/call
# spent 79µs making 30 calls to Module::Runtime::_is_string, avg 3µs/call
# spent 22µs making 1 call to Module::Runtime::CORE:regcomp |
220 | |||||
221 | =item is_valid_module_name(ARG) | ||||
222 | |||||
223 | Deprecated alias for L</is_module_name>. | ||||
224 | |||||
225 | =cut | ||||
226 | |||||
227 | 1 | 1µs | *is_valid_module_name = \&is_module_name; | ||
228 | |||||
229 | =item check_module_name(ARG) | ||||
230 | |||||
231 | Check whether I<ARG> is a plain string | ||||
232 | satisfying Perl module name syntax as described for L</$module_name_rx>. | ||||
233 | Return normally if it is, or C<die> if it is not. | ||||
234 | |||||
235 | =cut | ||||
236 | |||||
237 | # spent 392µs (69+323) within Module::Runtime::check_module_name which was called 30 times, avg 13µs/call:
# 30 times (69µs+323µs) by Module::Runtime::module_notional_filename at line 261, avg 13µs/call | ||||
238 | 30 | 64µs | 30 | 323µs | unless(&is_module_name) { # spent 323µs making 30 calls to Module::Runtime::is_module_name, avg 11µs/call |
239 | die +(_is_string($_[0]) ? "`$_[0]'" : "argument"). | ||||
240 | " is not a module name\n"; | ||||
241 | } | ||||
242 | } | ||||
243 | |||||
244 | =item module_notional_filename(NAME) | ||||
245 | |||||
246 | Generates a notional relative filename for a module, which is used in | ||||
247 | some Perl core interfaces. | ||||
248 | The I<NAME> is a string, which should be a valid module name (one or | ||||
249 | more C<::>-separated segments). If it is not a valid name, the function | ||||
250 | C<die>s. | ||||
251 | |||||
252 | The notional filename for the named module is generated and returned. | ||||
253 | This filename is always in Unix style, with C</> directory separators | ||||
254 | and a C<.pm> suffix. This kind of filename can be used as an argument to | ||||
255 | C<require>, and is the key that appears in C<%INC> to identify a module, | ||||
256 | regardless of actual local filename syntax. | ||||
257 | |||||
258 | =cut | ||||
259 | |||||
260 | # spent 587µs (135+452) within Module::Runtime::module_notional_filename which was called 30 times, avg 20µs/call:
# 30 times (135µs+452µs) by Module::Runtime::require_module at line 314, avg 20µs/call | ||||
261 | 30 | 36µs | 30 | 392µs | &check_module_name; # spent 392µs making 30 calls to Module::Runtime::check_module_name, avg 13µs/call |
262 | 30 | 9µs | my($name) = @_; | ||
263 | 30 | 92µs | 30 | 60µs | $name =~ s!::!/!g; # spent 60µs making 30 calls to Module::Runtime::CORE:subst, avg 2µs/call |
264 | 30 | 72µs | return $name.".pm"; | ||
265 | } | ||||
266 | |||||
267 | =item require_module(NAME) | ||||
268 | |||||
269 | This is essentially the bareword form of C<require>, in runtime form. | ||||
270 | The I<NAME> is a string, which should be a valid module name (one or | ||||
271 | more C<::>-separated segments). If it is not a valid name, the function | ||||
272 | C<die>s. | ||||
273 | |||||
274 | The module specified by I<NAME> is loaded, if it hasn't been already, | ||||
275 | in the manner of the bareword form of C<require>. That means that a | ||||
276 | search through C<@INC> is performed, and a byte-compiled form of the | ||||
277 | module will be used if available. | ||||
278 | |||||
279 | The return value is as for C<require>. That is, it is the value returned | ||||
280 | by the module itself if the module is loaded anew, or C<1> if the module | ||||
281 | was already loaded. | ||||
282 | |||||
283 | =cut | ||||
284 | |||||
285 | # Don't "use constant" here, to avoid dependencies. | ||||
286 | # spent 6µs within Module::Runtime::BEGIN@286 which was called:
# once (6µs+0s) by JSON::Schema::Modern::BEGIN@28 at line 291 | ||||
287 | *_WORK_AROUND_HINT_LEAKAGE = | ||||
288 | "$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001) | ||||
289 | 1 | 3µs | ? sub(){1} : sub(){0}; | ||
290 | 1 | 4µs | *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0}; | ||
291 | 1 | 28µs | 1 | 6µs | } # spent 6µs making 1 call to Module::Runtime::BEGIN@286 |
292 | |||||
293 | 1 | 3µs | # spent 3µs within Module::Runtime::BEGIN@293 which was called:
# once (3µs+0s) by JSON::Schema::Modern::BEGIN@28 at line 298 | ||
294 | sub Module::Runtime::__GUARD__::DESTROY { | ||||
295 | delete $INC{$_[0]->[0]} if @{$_[0]}; | ||||
296 | } | ||||
297 | 1; | ||||
298 | 1 | 369µs | 1 | 3µs | }; die $@ if $@ ne ""; } } # spent 3µs making 1 call to Module::Runtime::BEGIN@293 |
299 | |||||
300 | # spent 77.1ms (22.3+54.8) within Module::Runtime::require_module which was called 30 times, avg 2.57ms/call:
# 16 times (18.3ms+36.4ms) by Module::Runtime::use_module at line 345, avg 3.42ms/call
# 12 times (2.47ms+7.49ms) by MooX::HandlesVia::process_has at line 53 of MooX/HandlesVia.pm, avg 829µs/call
# 2 times (1.45ms+11.0ms) by Module::Implementation::try {...} at line 93 of Module/Implementation.pm, avg 6.20ms/call | ||||
301 | # Localise %^H to work around [perl #68590], where the bug exists | ||||
302 | # and this is a satisfactory workaround. The bug consists of | ||||
303 | # %^H state leaking into each required module, polluting the | ||||
304 | # module's lexical state. | ||||
305 | local %^H if _WORK_AROUND_HINT_LEAKAGE; | ||||
306 | 30 | 7µs | if(_WORK_AROUND_BROKEN_MODULE_STATE) { | ||
307 | my $notional_filename = &module_notional_filename; | ||||
308 | my $guard = bless([ $notional_filename ], | ||||
309 | "Module::Runtime::__GUARD__"); | ||||
310 | my $result = CORE::require($notional_filename); | ||||
311 | pop @$guard; | ||||
312 | return $result; | ||||
313 | } else { | ||||
314 | 30 | 6.49ms | 30 | 587µs | return scalar(CORE::require(&module_notional_filename)); # spent 587µs making 30 calls to Module::Runtime::module_notional_filename, avg 20µs/call |
315 | } | ||||
316 | } | ||||
317 | |||||
318 | =back | ||||
319 | |||||
320 | =head2 Structured module use | ||||
321 | |||||
322 | =over | ||||
323 | |||||
324 | =item use_module(NAME[, VERSION]) | ||||
325 | |||||
326 | This is essentially C<use> in runtime form, but without the importing | ||||
327 | feature (which is fundamentally a compile-time thing). The I<NAME> is | ||||
328 | handled just like in C<require_module> above: it must be a module name, | ||||
329 | and the named module is loaded as if by the bareword form of C<require>. | ||||
330 | |||||
331 | If a I<VERSION> is specified, the C<VERSION> method of the loaded module is | ||||
332 | called with the specified I<VERSION> as an argument. This normally serves to | ||||
333 | ensure that the version loaded is at least the version required. This is | ||||
334 | the same functionality provided by the I<VERSION> parameter of C<use>. | ||||
335 | |||||
336 | On success, the name of the module is returned. This is unlike | ||||
337 | L</require_module>, and is done so that the entire call to L</use_module> | ||||
338 | can be used as a class name to call a constructor, as in the example in | ||||
339 | the synopsis. | ||||
340 | |||||
341 | =cut | ||||
342 | |||||
343 | # spent 54.8ms (88µs+54.7) within Module::Runtime::use_module which was called 16 times, avg 3.43ms/call:
# 8 times (67µs+51.4ms) by JSON::Schema::Modern::__ANON__[/Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm:709] at line 705 of /Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm, avg 6.43ms/call
# 7 times (11µs+72µs) by JSON::Schema::Modern::__ANON__[/Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm:751] at line 743 of /Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm, avg 12µs/call
# once (10µs+3.28ms) by JSON::Schema::Modern::add_vocabulary at line 715 of /Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm | ||||
344 | 16 | 6µs | my($name, $version) = @_; | ||
345 | 16 | 35µs | 16 | 54.7ms | require_module($name); # spent 54.7ms making 16 calls to Module::Runtime::require_module, avg 3.42ms/call |
346 | 16 | 7µs | $name->VERSION($version) if @_ >= 2; | ||
347 | 16 | 33µs | return $name; | ||
348 | } | ||||
349 | |||||
350 | =item use_package_optimistically(NAME[, VERSION]) | ||||
351 | |||||
352 | This is an analogue of L</use_module> for the situation where there is | ||||
353 | uncertainty as to whether a package/class is defined in its own module | ||||
354 | or by some other means. It attempts to arrange for the named package to | ||||
355 | be available, either by loading a module or by doing nothing and hoping. | ||||
356 | |||||
357 | An attempt is made to load the named module (as if by the bareword form | ||||
358 | of C<require>). If the module cannot be found then it is assumed that | ||||
359 | the package was actually already loaded by other means, and no error | ||||
360 | is signalled. That's the optimistic bit. | ||||
361 | |||||
362 | I<Warning:> this optional module loading is liable to cause unreliable | ||||
363 | behaviour, including security problems. It interacts especially badly | ||||
364 | with having C<.> in C<@INC>, which was the default state of affairs in | ||||
365 | Perls prior to 5.25.11. If a package is actually defined by some means | ||||
366 | other than a module, then applying this function to it causes a spurious | ||||
367 | attempt to load a module that is expected to be non-existent. If a | ||||
368 | module actually exists under that name then it will be unintentionally | ||||
369 | loaded. If C<.> is in C<@INC> and this code is ever run with the current | ||||
370 | directory being one writable by a malicious user (such as F</tmp>), then | ||||
371 | the malicious user can easily cause the victim to run arbitrary code, by | ||||
372 | creating a module file under the predictable spuriously-loaded name in the | ||||
373 | writable directory. Generally, optional module loading should be avoided. | ||||
374 | |||||
375 | This is mostly the same operation that is performed by the L<base> pragma | ||||
376 | to ensure that the specified base classes are available. The behaviour | ||||
377 | of L<base> was simplified in version 2.18, and later improved in version | ||||
378 | 2.20, and on both occasions this function changed to match. | ||||
379 | |||||
380 | If a I<VERSION> is specified, the C<VERSION> method of the loaded package is | ||||
381 | called with the specified I<VERSION> as an argument. This normally serves | ||||
382 | to ensure that the version loaded is at least the version required. | ||||
383 | On success, the name of the package is returned. These aspects of the | ||||
384 | function work just like L</use_module>. | ||||
385 | |||||
386 | =cut | ||||
387 | |||||
388 | sub use_package_optimistically($;$) { | ||||
389 | my($name, $version) = @_; | ||||
390 | my $fn = module_notional_filename($name); | ||||
391 | eval { local $SIG{__DIE__}; require_module($name); }; | ||||
392 | die $@ if $@ ne "" && | ||||
393 | ($@ !~ /\ACan't locate \Q$fn\E .+ at \Q@{[__FILE__]}\E line/s || | ||||
394 | $@ =~ /^Compilation\ failed\ in\ require | ||||
395 | \ at\ \Q@{[__FILE__]}\E\ line/xm); | ||||
396 | $name->VERSION($version) if @_ >= 2; | ||||
397 | return $name; | ||||
398 | } | ||||
399 | |||||
400 | =back | ||||
401 | |||||
402 | =head2 Module name composition | ||||
403 | |||||
404 | =over | ||||
405 | |||||
406 | =item is_module_spec(PREFIX, SPEC) | ||||
407 | |||||
408 | Returns a truth value indicating | ||||
409 | whether I<SPEC> is valid input for L</compose_module_name>. | ||||
410 | See below for what that entails. Whether a I<PREFIX> is supplied affects | ||||
411 | the validity of I<SPEC>, but the exact value of the prefix is unimportant, | ||||
412 | so this function treats I<PREFIX> as a truth value. | ||||
413 | |||||
414 | =cut | ||||
415 | |||||
416 | sub is_module_spec($$) { | ||||
417 | my($prefix, $spec) = @_; | ||||
418 | return _is_string($spec) && | ||||
419 | $spec =~ ($prefix ? qr/\A$sub_module_spec_rx\z/o : | ||||
420 | qr/\A$top_module_spec_rx\z/o); | ||||
421 | } | ||||
422 | |||||
423 | =item is_valid_module_spec(PREFIX, SPEC) | ||||
424 | |||||
425 | Deprecated alias for L</is_module_spec>. | ||||
426 | |||||
427 | =cut | ||||
428 | |||||
429 | 1 | 1µs | *is_valid_module_spec = \&is_module_spec; | ||
430 | |||||
431 | =item check_module_spec(PREFIX, SPEC) | ||||
432 | |||||
433 | Check whether I<SPEC> is valid input for L</compose_module_name>. | ||||
434 | Return normally if it is, or C<die> if it is not. | ||||
435 | |||||
436 | =cut | ||||
437 | |||||
438 | sub check_module_spec($$) { | ||||
439 | unless(&is_module_spec) { | ||||
440 | die +(_is_string($_[1]) ? "`$_[1]'" : "argument"). | ||||
441 | " is not a module specification\n"; | ||||
442 | } | ||||
443 | } | ||||
444 | |||||
445 | =item compose_module_name(PREFIX, SPEC) | ||||
446 | |||||
447 | This function is intended to make it more convenient for a user to specify | ||||
448 | a Perl module name at runtime. Users have greater need for abbreviations | ||||
449 | and context-sensitivity than programmers, and Perl module names get a | ||||
450 | little unwieldy. I<SPEC> is what the user specifies, and this function | ||||
451 | translates it into a module name in standard form, which it returns. | ||||
452 | |||||
453 | I<SPEC> has syntax approximately that of a standard module name: it | ||||
454 | should consist of one or more name segments, each of which consists | ||||
455 | of one or more identifier characters. However, C</> is permitted as a | ||||
456 | separator, in addition to the standard C<::>. The two separators are | ||||
457 | entirely interchangeable. | ||||
458 | |||||
459 | Additionally, if I<PREFIX> is not C<undef> then it must be a module | ||||
460 | name in standard form, and it is prefixed to the user-specified name. | ||||
461 | The user can inhibit the prefix addition by starting I<SPEC> with a | ||||
462 | separator (either C</> or C<::>). | ||||
463 | |||||
464 | =cut | ||||
465 | |||||
466 | sub compose_module_name($$) { | ||||
467 | my($prefix, $spec) = @_; | ||||
468 | check_module_name($prefix) if defined $prefix; | ||||
469 | &check_module_spec; | ||||
470 | if($spec =~ s#\A(?:/|::)##) { | ||||
471 | # OK | ||||
472 | } else { | ||||
473 | $spec = $prefix."::".$spec if defined $prefix; | ||||
474 | } | ||||
475 | $spec =~ s#/#::#g; | ||||
476 | return $spec; | ||||
477 | } | ||||
478 | |||||
479 | =back | ||||
480 | |||||
481 | =head1 BUGS | ||||
482 | |||||
483 | On Perl versions 5.7.2 to 5.8.8, if C<require> is overridden by the | ||||
484 | C<CORE::GLOBAL> mechanism, it is likely to break the heuristics used by | ||||
485 | L</use_package_optimistically>, making it signal an error for a missing | ||||
486 | module rather than assume that it was already loaded. From Perl 5.8.9 | ||||
487 | onwards, and on 5.7.1 and earlier, this module can avoid being confused | ||||
488 | by such an override. On the affected versions, a C<require> override | ||||
489 | might be installed by L<Lexical::SealRequireHints>, if something requires | ||||
490 | its bugfix but for some reason its XS implementation isn't available. | ||||
491 | |||||
492 | =head1 SEE ALSO | ||||
493 | |||||
494 | L<Lexical::SealRequireHints>, | ||||
495 | L<base>, | ||||
496 | L<perlfunc/require>, | ||||
497 | L<perlfunc/use> | ||||
498 | |||||
499 | =head1 AUTHOR | ||||
500 | |||||
501 | Andrew Main (Zefram) <zefram@fysh.org> | ||||
502 | |||||
503 | =head1 COPYRIGHT | ||||
504 | |||||
505 | Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012, 2014, 2017 | ||||
506 | Andrew Main (Zefram) <zefram@fysh.org> | ||||
507 | |||||
508 | =head1 LICENSE | ||||
509 | |||||
510 | This module is free software; you can redistribute it and/or modify it | ||||
511 | under the same terms as Perl itself. | ||||
512 | |||||
513 | =cut | ||||
514 | |||||
515 | 1 | 12µs | 1; | ||
sub Module::Runtime::CORE:match; # opcode | |||||
# spent 2µs within Module::Runtime::CORE:qr which was called 6 times, avg 333ns/call:
# once (2µs+0s) by JSON::Schema::Modern::BEGIN@28 at line 176
# once (0s+0s) by JSON::Schema::Modern::BEGIN@28 at line 188
# once (0s+0s) by JSON::Schema::Modern::BEGIN@28 at line 185
# once (0s+0s) by JSON::Schema::Modern::BEGIN@28 at line 202
# once (0s+0s) by JSON::Schema::Modern::BEGIN@28 at line 191
# once (0s+0s) by JSON::Schema::Modern::BEGIN@28 at line 200 | |||||
sub Module::Runtime::CORE:regcomp; # opcode | |||||
# spent 60µs within Module::Runtime::CORE:subst which was called 30 times, avg 2µs/call:
# 30 times (60µs+0s) by Module::Runtime::module_notional_filename at line 263, avg 2µs/call |