Filename | /Users/dde/perl5/perlbrew/perls/5.18.0t/lib/site_perl/5.18.0/Class/Load.pm |
Statements | Executed 3710 statements in 8.44ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
321 | 2 | 1 | 5.99ms | 162ms | try_load_class (recurses: max depth 3, inclusive time 70.8ms) | Class::Load::
320 | 12 | 10 | 1.07ms | 162ms | load_class (recurses: max depth 3, inclusive time 70.9ms) | Class::Load::
1 | 1 | 1 | 805µs | 895µs | BEGIN@7 | Class::Load::
1 | 1 | 1 | 738µs | 4.67ms | BEGIN@9 | Class::Load::
1 | 1 | 1 | 567µs | 5.29ms | BEGIN@8 | Class::Load::
2 | 1 | 1 | 49µs | 299µs | load_first_existing_class | Class::Load::
1 | 1 | 1 | 10µs | 38µs | BEGIN@10 | Class::Load::
1 | 1 | 1 | 10µs | 20µs | BEGIN@5 | Class::Load::
1 | 1 | 1 | 8µs | 12µs | _error | Class::Load::
1 | 1 | 1 | 8µs | 8µs | CORE:regcomp (opcode) | Class::Load::
1 | 1 | 1 | 6µs | 31µs | BEGIN@16 | Class::Load::
1 | 1 | 1 | 6µs | 9µs | BEGIN@6 | Class::Load::
1 | 1 | 1 | 4µs | 4µs | CORE:subst (opcode) | Class::Load::
1 | 1 | 1 | 800ns | 800ns | CORE:match (opcode) | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:152] | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:155] | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:180] | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:183] | Class::Load::
0 | 0 | 0 | 0s | 0s | _croak | Class::Load::
0 | 0 | 0 | 0s | 0s | _nonexistent_fail_re | Class::Load::
0 | 0 | 0 | 0s | 0s | _or_list | Class::Load::
0 | 0 | 0 | 0s | 0s | _version_fail_re | Class::Load::
0 | 0 | 0 | 0s | 0s | load_optional_class | Class::Load::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Class::Load; | ||||
2 | { | ||||
3 | 2 | 800ns | $Class::Load::VERSION = '0.20'; | ||
4 | } | ||||
5 | 2 | 19µs | 2 | 30µs | # spent 20µs (10+10) within Class::Load::BEGIN@5 which was called:
# once (10µs+10µs) by Moose::BEGIN@15 at line 5 # spent 20µs making 1 call to Class::Load::BEGIN@5
# spent 10µs making 1 call to strict::import |
6 | 2 | 18µs | 2 | 12µs | # spent 9µs (6+3) within Class::Load::BEGIN@6 which was called:
# once (6µs+3µs) by Moose::BEGIN@15 at line 6 # spent 9µs making 1 call to Class::Load::BEGIN@6
# spent 3µs making 1 call to warnings::import |
7 | 2 | 84µs | 2 | 948µs | # spent 895µs (805+90) within Class::Load::BEGIN@7 which was called:
# once (805µs+90µs) by Moose::BEGIN@15 at line 7 # spent 895µs making 1 call to Class::Load::BEGIN@7
# spent 53µs making 1 call to base::import |
8 | 2 | 79µs | 2 | 5.35ms | # spent 5.29ms (567µs+4.72) within Class::Load::BEGIN@8 which was called:
# once (567µs+4.72ms) by Moose::BEGIN@15 at line 8 # spent 5.29ms making 1 call to Class::Load::BEGIN@8
# spent 62µs making 1 call to Sub::Install::__ANON__[Sub/Install.pm:284] |
9 | 3 | 123µs | 2 | 4.68ms | # spent 4.67ms (738µs+3.93) within Class::Load::BEGIN@9 which was called:
# once (738µs+3.93ms) by Moose::BEGIN@15 at line 9 # spent 4.67ms making 1 call to Class::Load::BEGIN@9
# spent 8µs making 1 call to UNIVERSAL::VERSION |
10 | 1 | 200ns | # spent 38µs (10+29) within Class::Load::BEGIN@10 which was called:
# once (10µs+29µs) by Moose::BEGIN@15 at line 15 | ||
11 | check_module_name | ||||
12 | module_notional_filename | ||||
13 | require_module | ||||
14 | use_module | ||||
15 | 2 | 32µs | 3 | 67µs | ); # spent 38µs making 1 call to Class::Load::BEGIN@10
# spent 22µs making 1 call to Module::Runtime::import
# spent 7µs making 1 call to UNIVERSAL::VERSION |
16 | 2 | 822µs | 2 | 56µs | # spent 31µs (6+25) within Class::Load::BEGIN@16 which was called:
# once (6µs+25µs) by Moose::BEGIN@15 at line 16 # spent 31µs making 1 call to Class::Load::BEGIN@16
# spent 25µs making 1 call to Exporter::import |
17 | |||||
18 | { | ||||
19 | 2 | 3µs | 1 | 24µs | my $loader = Module::Implementation::build_loader_sub( # spent 24µs making 1 call to Module::Implementation::build_loader_sub |
20 | implementations => [ 'XS', 'PP' ], | ||||
21 | symbols => ['is_class_loaded'], | ||||
22 | ); | ||||
23 | |||||
24 | 1 | 4µs | 1 | 542µs | $loader->(); # spent 542µs making 1 call to Module::Implementation::__ANON__[Module/Implementation.pm:47] |
25 | } | ||||
26 | |||||
27 | 1 | 1µs | our @EXPORT_OK = qw/load_class load_optional_class try_load_class is_class_loaded load_first_existing_class/; | ||
28 | 1 | 700ns | our %EXPORT_TAGS = ( | ||
29 | all => \@EXPORT_OK, | ||||
30 | ); | ||||
31 | |||||
32 | 1 | 0s | our $ERROR; | ||
33 | |||||
34 | # spent 162ms (1.07+161) within Class::Load::load_class which was called 320 times, avg 508µs/call:
# 148 times (443µs+10.4ms) by Moose::Meta::Class::_inline_create_error at line 843 of Moose/Meta/Class.pm, avg 73µs/call
# 52 times (189µs+90.9ms) by Moose::Meta::Attribute::Native::Trait::_native_accessor_class_for at line 204 of Moose/Meta/Attribute/Native/Trait.pm, avg 1.75ms/call
# 31 times (93µs+560µs) by Class::MOP::Class::_inline_constructor at line 1436 of Class/MOP/Class.pm, avg 21µs/call
# 26 times (119µs+17.4ms) by Moose::Util::_apply_all_roles at line 132 of Moose/Util.pm, avg 674µs/call
# 21 times (60µs+124µs) by Moose::Meta::Role::apply at line 459 of Moose/Meta/Role.pm, avg 9µs/call
# 20 times (62µs+295µs) by Moose::Meta::Class::superclasses at line 564 of Moose/Meta/Class.pm, avg 18µs/call
# 14 times (62µs+1.61ms) by Class::MOP::MiniTrait::apply at line 18 of Class/MOP/MiniTrait.pm, avg 119µs/call
# 3 times (9µs+12µs) by Moose::Meta::Role::Composite::apply_params at line 130 of Moose/Meta/Role/Composite.pm, avg 7µs/call
# 2 times (15µs+27.5ms) by MooseX::Storage::_expand_role at line 77 of Class/MOP.pm, avg 13.8ms/call
# once (7µs+12.5ms) by Moose::Meta::Attribute::Custom::Trait::Array::register_implementation at line 28 of Moose/Meta/Attribute/Native.pm
# once (3µs+18µs) by metaclass::import at line 32 of metaclass.pm
# once (3µs+18µs) by Class::MOP::Class::_inline_destructor at line 1473 of Class/MOP/Class.pm | ||||
35 | 320 | 44µs | my $class = shift; | ||
36 | 320 | 32µs | my $options = shift; | ||
37 | |||||
38 | 320 | 325µs | 320 | 161ms | my ($res, $e) = try_load_class($class, $options); # spent 232ms making 320 calls to Class::Load::try_load_class, avg 726µs/call, recursion: max depth 3, sum of overlapping time 70.8ms |
39 | 320 | 430µs | return $class if $res; | ||
40 | |||||
41 | _croak($e); | ||||
42 | } | ||||
43 | |||||
44 | # spent 299µs (49+250) within Class::Load::load_first_existing_class which was called 2 times, avg 150µs/call:
# 2 times (49µs+250µs) by Moose::Util::resolve_metaclass_alias at line 225 of Moose/Util.pm, avg 150µs/call | ||||
45 | 2 | 2µs | 2 | 32µs | my $classes = Data::OptList::mkopt(\@_) # spent 32µs making 2 calls to Data::OptList::mkopt, avg 16µs/call |
46 | or return; | ||||
47 | |||||
48 | 2 | 1µs | foreach my $class (@{$classes}) { | ||
49 | 4 | 6µs | 4 | 38µs | check_module_name($class->[0]); # spent 38µs making 4 calls to Module::Runtime::check_module_name, avg 9µs/call |
50 | } | ||||
51 | |||||
52 | 2 | 1µs | for my $class (@{$classes}) { | ||
53 | 3 | 2µs | my ($name, $options) = @{$class}; | ||
54 | |||||
55 | # We need to be careful not to pass an undef $options to this sub, | ||||
56 | # since the XS version will blow up if that happens. | ||||
57 | 3 | 25µs | 3 | 12µs | return $name if is_class_loaded($name, ($options ? $options : ())); # spent 12µs making 3 calls to Class::Load::XS::is_class_loaded, avg 4µs/call |
58 | |||||
59 | 1 | 5µs | 1 | 142µs | my ($res, $e) = try_load_class($name, $options); # spent 142µs making 1 call to Class::Load::try_load_class |
60 | |||||
61 | 1 | 200ns | return $name if $res; | ||
62 | |||||
63 | 1 | 1µs | 1 | 17µs | my $file = module_notional_filename($name); # spent 17µs making 1 call to Module::Runtime::module_notional_filename |
64 | |||||
65 | 1 | 17µs | 2 | 9µs | next if $e =~ /^Can't locate \Q$file\E in \@INC/; # spent 8µs making 1 call to Class::Load::CORE:regcomp
# spent 800ns making 1 call to Class::Load::CORE:match |
66 | next | ||||
67 | if $options | ||||
68 | && defined $options->{-version} | ||||
69 | && $e =~ _version_fail_re($name, $options->{-version}); | ||||
70 | |||||
71 | _croak("Couldn't load class ($name) because: $e"); | ||||
72 | } | ||||
73 | |||||
74 | my @list = map { | ||||
75 | $_->[0] | ||||
76 | . ( $_->[1] && defined $_->[1]{-version} | ||||
77 | ? " (version >= $_->[1]{-version})" | ||||
78 | : q{} ) | ||||
79 | } @{$classes}; | ||||
80 | |||||
81 | my $err | ||||
82 | .= q{Can't locate } | ||||
83 | . _or_list(@list) | ||||
84 | . " in \@INC (\@INC contains: @INC)."; | ||||
85 | _croak($err); | ||||
86 | } | ||||
87 | |||||
88 | sub _version_fail_re { | ||||
89 | my $name = shift; | ||||
90 | my $vers = shift; | ||||
91 | |||||
92 | return qr/\Q$name\E version \Q$vers\E required--this is only version/; | ||||
93 | } | ||||
94 | |||||
95 | sub _nonexistent_fail_re { | ||||
96 | my $name = shift; | ||||
97 | |||||
98 | my $file = module_notional_filename($name); | ||||
99 | return qr/Can't locate \Q$file\E in \@INC/; | ||||
100 | } | ||||
101 | |||||
102 | sub _or_list { | ||||
103 | return $_[0] if @_ == 1; | ||||
104 | |||||
105 | return join ' or ', @_ if @_ ==2; | ||||
106 | |||||
107 | my $last = pop; | ||||
108 | |||||
109 | my $list = join ', ', @_; | ||||
110 | $list .= ', or ' . $last; | ||||
111 | |||||
112 | return $list; | ||||
113 | } | ||||
114 | |||||
115 | sub load_optional_class { | ||||
116 | my $class = shift; | ||||
117 | my $options = shift; | ||||
118 | |||||
119 | check_module_name($class); | ||||
120 | |||||
121 | my ($res, $e) = try_load_class($class, $options); | ||||
122 | return 1 if $res; | ||||
123 | |||||
124 | return 0 | ||||
125 | if $options | ||||
126 | && defined $options->{-version} | ||||
127 | && $e =~ _version_fail_re($class, $options->{-version}); | ||||
128 | |||||
129 | return 0 | ||||
130 | if $e =~ _nonexistent_fail_re($class); | ||||
131 | |||||
132 | _croak($e); | ||||
133 | } | ||||
134 | |||||
135 | sub try_load_class { | ||||
136 | 321 | 41µs | my $class = shift; | ||
137 | 321 | 2µs | my $options = shift; | ||
138 | |||||
139 | 321 | 283µs | 321 | 7.15ms | check_module_name($class); # spent 7.15ms making 321 calls to Module::Runtime::check_module_name, avg 22µs/call |
140 | |||||
141 | 321 | 31µs | local $@; | ||
142 | 321 | 33µs | undef $ERROR; | ||
143 | |||||
144 | 321 | 5.15ms | 321 | 532µs | if (is_class_loaded($class)) { # spent 532µs making 321 calls to Class::Load::XS::is_class_loaded, avg 2µs/call |
145 | # We need to check this here rather than in is_class_loaded() because | ||||
146 | # we want to return the error message for a failed version check, but | ||||
147 | # is_class_loaded just returns true/false. | ||||
148 | 299 | 488µs | return 1 unless $options && defined $options->{-version}; | ||
149 | return try { | ||||
150 | $class->VERSION($options->{-version}); | ||||
151 | 1; | ||||
152 | } | ||||
153 | catch { | ||||
154 | _error($_); | ||||
155 | }; | ||||
156 | } | ||||
157 | |||||
158 | 22 | 25µs | 22 | 357µs | my $file = module_notional_filename($class); # spent 357µs making 22 calls to Module::Runtime::module_notional_filename, avg 16µs/call |
159 | # This says "our diagnostics of the package | ||||
160 | # say perl's INC status about the file being loaded are | ||||
161 | # wrong", so we delete it from %INC, so when we call require(), | ||||
162 | # perl will *actually* try reloading the file. | ||||
163 | # | ||||
164 | # If the file is already in %INC, it won't retry, | ||||
165 | # And on 5.8, it won't fail either! | ||||
166 | # | ||||
167 | # The extra benefit of this trick, is it helps even on | ||||
168 | # 5.10, as instead of dying with "Compilation failed", | ||||
169 | # it will die with the actual error, and thats a win-win. | ||||
170 | 22 | 13µs | delete $INC{$file}; | ||
171 | return try { | ||||
172 | 22 | 39µs | local $SIG{__DIE__} = 'DEFAULT'; | ||
173 | 22 | 19µs | if ($options && defined $options->{-version}) { | ||
174 | use_module($class, $options->{-version}); | ||||
175 | } | ||||
176 | else { | ||||
177 | 22 | 19µs | 22 | 148ms | require_module($class); # spent 218ms making 22 calls to Module::Runtime::require_module, avg 9.90ms/call, recursion: max depth 3, sum of overlapping time 69.9ms |
178 | } | ||||
179 | 21 | 56µs | 1; | ||
180 | } | ||||
181 | catch { | ||||
182 | 1 | 4µs | 1 | 12µs | _error($_); # spent 12µs making 1 call to Class::Load::_error |
183 | 22 | 146µs | 44 | 58.4ms | }; # spent 218ms making 22 calls to Try::Tiny::try, avg 9.92ms/call, recursion: max depth 4, sum of overlapping time 160ms
# spent 64µs making 22 calls to Try::Tiny::catch, avg 3µs/call |
184 | } | ||||
185 | |||||
186 | # spent 12µs (8+4) within Class::Load::_error which was called:
# once (8µs+4µs) by Class::Load::catch {...} at line 182 | ||||
187 | 1 | 500ns | my $e = shift; | ||
188 | |||||
189 | 1 | 8µs | 1 | 4µs | $e =~ s/ at .+?Runtime\.pm line [0-9]+\.$//; # spent 4µs making 1 call to Class::Load::CORE:subst |
190 | 1 | 700ns | chomp $e; | ||
191 | |||||
192 | 1 | 500ns | $ERROR = $e; | ||
193 | 1 | 200ns | return 0 unless wantarray; | ||
194 | 1 | 3µs | return 0, $ERROR; | ||
195 | } | ||||
196 | |||||
197 | sub _croak { | ||||
198 | require Carp; | ||||
199 | local $Carp::CarpLevel = $Carp::CarpLevel + 2; | ||||
200 | Carp::croak(shift); | ||||
201 | } | ||||
202 | |||||
203 | 1 | 5µs | 1; | ||
204 | |||||
205 | # ABSTRACT: a working (require "Class::Name") and more | ||||
206 | |||||
- - | |||||
209 | =pod | ||||
210 | |||||
211 | =head1 NAME | ||||
212 | |||||
213 | Class::Load - a working (require "Class::Name") and more | ||||
214 | |||||
215 | =head1 VERSION | ||||
216 | |||||
217 | version 0.20 | ||||
218 | |||||
219 | =head1 SYNOPSIS | ||||
220 | |||||
221 | use Class::Load ':all'; | ||||
222 | |||||
223 | try_load_class('Class::Name') | ||||
224 | or plan skip_all => "Class::Name required to run these tests"; | ||||
225 | |||||
226 | load_class('Class::Name'); | ||||
227 | |||||
228 | is_class_loaded('Class::Name'); | ||||
229 | |||||
230 | my $baseclass = load_optional_class('Class::Name::MightExist') | ||||
231 | ? 'Class::Name::MightExist' | ||||
232 | : 'Class::Name::Default'; | ||||
233 | |||||
234 | =head1 DESCRIPTION | ||||
235 | |||||
236 | C<require EXPR> only accepts C<Class/Name.pm> style module names, not | ||||
237 | C<Class::Name>. How frustrating! For that, we provide | ||||
238 | C<load_class 'Class::Name'>. | ||||
239 | |||||
240 | It's often useful to test whether a module can be loaded, instead of throwing | ||||
241 | an error when it's not available. For that, we provide | ||||
242 | C<try_load_class 'Class::Name'>. | ||||
243 | |||||
244 | Finally, sometimes we need to know whether a particular class has been loaded. | ||||
245 | Asking C<%INC> is an option, but that will miss inner packages and any class | ||||
246 | for which the filename does not correspond to the package name. For that, we | ||||
247 | provide C<is_class_loaded 'Class::Name'>. | ||||
248 | |||||
249 | =head1 FUNCTIONS | ||||
250 | |||||
251 | =head2 load_class Class::Name, \%options | ||||
252 | |||||
253 | C<load_class> will load C<Class::Name> or throw an error, much like C<require>. | ||||
254 | |||||
255 | If C<Class::Name> is already loaded (checked with C<is_class_loaded>) then it | ||||
256 | will not try to load the class. This is useful when you have inner packages | ||||
257 | which C<require> does not check. | ||||
258 | |||||
259 | The C<%options> hash currently accepts one key, C<-version>. If you specify a | ||||
260 | version, then this subroutine will call C<< Class::Name->VERSION( | ||||
261 | $options{-version} ) >> internally, which will throw an error if the class's | ||||
262 | version is not equal to or greater than the version you requested. | ||||
263 | |||||
264 | This method will return the name of the class on success. | ||||
265 | |||||
266 | =head2 try_load_class Class::Name, \%options -> (0|1, error message) | ||||
267 | |||||
268 | Returns 1 if the class was loaded, 0 if it was not. If the class was not | ||||
269 | loaded, the error will be returned as a second return value in list context. | ||||
270 | |||||
271 | Again, if C<Class::Name> is already loaded (checked with C<is_class_loaded>) | ||||
272 | then it will not try to load the class. This is useful when you have inner | ||||
273 | packages which C<require> does not check. | ||||
274 | |||||
275 | Like C<load_class>, you can pass a C<-version> in C<%options>. If the version | ||||
276 | is not sufficient, then this subroutine will return false. | ||||
277 | |||||
278 | =head2 is_class_loaded Class::Name, \%options -> 0|1 | ||||
279 | |||||
280 | This uses a number of heuristics to determine if the class C<Class::Name> is | ||||
281 | loaded. There heuristics were taken from L<Class::MOP>'s old pure-perl | ||||
282 | implementation. | ||||
283 | |||||
284 | Like C<load_class>, you can pass a C<-version> in C<%options>. If the version | ||||
285 | is not sufficient, then this subroutine will return false. | ||||
286 | |||||
287 | =head2 load_first_existing_class Class::Name, \%options, ... | ||||
288 | |||||
289 | This attempts to load the first loadable class in the list of classes | ||||
290 | given. Each class name can be followed by an options hash reference. | ||||
291 | |||||
292 | If any one of the classes loads and passes the optional version check, that | ||||
293 | class name will be returned. If I<none> of the classes can be loaded (or none | ||||
294 | pass their version check), then an error will be thrown. | ||||
295 | |||||
296 | If, when attempting to load a class, it fails to load because of a syntax | ||||
297 | error, then an error will be thrown immediately. | ||||
298 | |||||
299 | =head2 load_optional_class Class::Name, \%options -> 0|1 | ||||
300 | |||||
301 | C<load_optional_class> is a lot like C<try_load_class>, but also a lot like | ||||
302 | C<load_class>. | ||||
303 | |||||
304 | If the class exists, and it works, then it will return 1. If you specify a | ||||
305 | version in C<%options>, then the version check must succeed or it will return | ||||
306 | 0. | ||||
307 | |||||
308 | If the class doesn't exist, and it appears to not exist on disk either, it | ||||
309 | will return 0. | ||||
310 | |||||
311 | If the class exists on disk, but loading from disk results in an error | ||||
312 | ( i.e.: a syntax error ), then it will C<croak> with that error. | ||||
313 | |||||
314 | This is useful for using if you want a fallback module system, i.e.: | ||||
315 | |||||
316 | my $class = load_optional_class($foo) ? $foo : $default; | ||||
317 | |||||
318 | That way, if $foo does exist, but can't be loaded due to error, you won't | ||||
319 | get the behaviour of it simply not existing. | ||||
320 | |||||
321 | =head1 SEE ALSO | ||||
322 | |||||
323 | =over 4 | ||||
324 | |||||
325 | =item L<http://blog.fox.geek.nz/2010/11/searching-design-spec-for-ultimate.html> | ||||
326 | |||||
327 | This blog post is a good overview of the current state of the existing modules | ||||
328 | for loading other modules in various ways. | ||||
329 | |||||
330 | =item L<http://blog.fox.geek.nz/2010/11/handling-optional-requirements-with.html> | ||||
331 | |||||
332 | This blog post describes how to handle optional modules with L<Class::Load>. | ||||
333 | |||||
334 | =item L<http://d.hatena.ne.jp/tokuhirom/20110202/1296598578> | ||||
335 | |||||
336 | This Japanese blog post describes why L<DBIx::Skinny> now uses L<Class::Load> | ||||
337 | over its competitors. | ||||
338 | |||||
339 | =item L<Moose>, L<Jifty>, L<Prophet>, etc | ||||
340 | |||||
341 | This module was designed to be used anywhere you have | ||||
342 | C<if (eval "require $module"; 1)>, which occurs in many large projects. | ||||
343 | |||||
344 | =back | ||||
345 | |||||
346 | =head1 AUTHOR | ||||
347 | |||||
348 | Shawn M Moore <sartak at bestpractical.com> | ||||
349 | |||||
350 | =head1 COPYRIGHT AND LICENSE | ||||
351 | |||||
352 | This software is copyright (c) 2012 by Shawn M Moore. | ||||
353 | |||||
354 | This is free software; you can redistribute it and/or modify it under | ||||
355 | the same terms as the Perl 5 programming language system itself. | ||||
356 | |||||
357 | =cut | ||||
358 | |||||
359 | |||||
360 | __END__ | ||||
# spent 800ns within Class::Load::CORE:match which was called:
# once (800ns+0s) by Class::Load::load_first_existing_class at line 65 | |||||
# spent 8µs within Class::Load::CORE:regcomp which was called:
# once (8µs+0s) by Class::Load::load_first_existing_class at line 65 | |||||
# spent 4µs within Class::Load::CORE:subst which was called:
# once (4µs+0s) by Class::Load::_error at line 189 |