Filename | /usr/share/perl5/Class/Load.pm |
Statements | Executed 26 statements in 2.51ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.86ms | 10.5ms | BEGIN@19 | Class::Load::
1 | 1 | 1 | 1.28ms | 6.59ms | BEGIN@10 | Class::Load::
1 | 1 | 1 | 14µs | 26µs | BEGIN@1 | DateTime::TimeZone::Local::
1 | 1 | 1 | 10µs | 18µs | BEGIN@11 | Class::Load::
1 | 1 | 1 | 10µs | 39µs | BEGIN@12 | Class::Load::
1 | 1 | 1 | 7µs | 71µs | BEGIN@9 | Class::Load::
1 | 1 | 1 | 7µs | 11µs | BEGIN@2 | DateTime::TimeZone::Local::
1 | 1 | 1 | 7µs | 33µs | BEGIN@18 | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:155] | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:158] | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:183] | Class::Load::
0 | 0 | 0 | 0s | 0s | __ANON__[:186] | Class::Load::
0 | 0 | 0 | 0s | 0s | _croak | Class::Load::
0 | 0 | 0 | 0s | 0s | _error | 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_class | Class::Load::
0 | 0 | 0 | 0s | 0s | load_first_existing_class | Class::Load::
0 | 0 | 0 | 0s | 0s | load_optional_class | Class::Load::
0 | 0 | 0 | 0s | 0s | try_load_class | Class::Load::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | 2 | 23µs | 2 | 38µs | # spent 26µs (14+12) within DateTime::TimeZone::Local::BEGIN@1 which was called:
# once (14µs+12µs) by DateTime::TimeZone::Local::BEGIN@6 at line 1 # spent 26µs making 1 call to DateTime::TimeZone::Local::BEGIN@1
# spent 12µs making 1 call to strict::import |
2 | 2 | 46µs | 2 | 15µs | # spent 11µs (7+4) within DateTime::TimeZone::Local::BEGIN@2 which was called:
# once (7µs+4µs) by DateTime::TimeZone::Local::BEGIN@6 at line 2 # spent 11µs making 1 call to DateTime::TimeZone::Local::BEGIN@2
# spent 4µs making 1 call to warnings::import |
3 | package Class::Load; | ||||
4 | # git description: v0.21-14-g8465ff7 | ||||
5 | 1 | 300ns | $Class::Load::VERSION = '0.22'; | ||
6 | # ABSTRACT: A working (require "Class::Name") and more | ||||
7 | # KEYWORDS: class module load require use runtime | ||||
8 | |||||
9 | 2 | 34µs | 2 | 134µs | # spent 71µs (7+63) within Class::Load::BEGIN@9 which was called:
# once (7µs+63µs) by DateTime::TimeZone::Local::BEGIN@6 at line 9 # spent 71µs making 1 call to Class::Load::BEGIN@9
# spent 63µs making 1 call to base::import |
10 | 2 | 751µs | 2 | 6.65ms | # spent 6.59ms (1.28+5.31) within Class::Load::BEGIN@10 which was called:
# once (1.28ms+5.31ms) by DateTime::TimeZone::Local::BEGIN@6 at line 10 # spent 6.59ms making 1 call to Class::Load::BEGIN@10
# spent 58µs making 1 call to Sub::Install::__ANON__[Sub/Install.pm:270] |
11 | 3 | 44µs | 2 | 26µs | # spent 18µs (10+8) within Class::Load::BEGIN@11 which was called:
# once (10µs+8µs) by DateTime::TimeZone::Local::BEGIN@6 at line 11 # spent 18µs making 1 call to Class::Load::BEGIN@11
# spent 8µs making 1 call to UNIVERSAL::VERSION |
12 | 1 | 3µs | 1 | 22µs | # spent 39µs (10+30) within Class::Load::BEGIN@12 which was called:
# once (10µs+30µs) by DateTime::TimeZone::Local::BEGIN@6 at line 17 # spent 22µs making 1 call to Module::Runtime::import |
13 | check_module_name | ||||
14 | module_notional_filename | ||||
15 | require_module | ||||
16 | use_module | ||||
17 | 2 | 32µs | 2 | 46µs | ); # spent 39µs making 1 call to Class::Load::BEGIN@12
# spent 7µs making 1 call to UNIVERSAL::VERSION |
18 | 2 | 21µs | 2 | 59µs | # spent 33µs (7+26) within Class::Load::BEGIN@18 which was called:
# once (7µs+26µs) by DateTime::TimeZone::Local::BEGIN@6 at line 18 # spent 33µs making 1 call to Class::Load::BEGIN@18
# spent 26µs making 1 call to Exporter::import |
19 | 2 | 1.53ms | 2 | 10.7ms | # spent 10.5ms (1.86+8.63) within Class::Load::BEGIN@19 which was called:
# once (1.86ms+8.63ms) by DateTime::TimeZone::Local::BEGIN@6 at line 19 # spent 10.5ms making 1 call to Class::Load::BEGIN@19
# spent 226µs making 1 call to namespace::clean::import |
20 | |||||
21 | { | ||||
22 | 2 | 3µs | 1 | 16µs | my $loader = Module::Implementation::build_loader_sub( # spent 16µs making 1 call to Module::Implementation::build_loader_sub |
23 | implementations => [ 'XS', 'PP' ], | ||||
24 | symbols => ['is_class_loaded'], | ||||
25 | ); | ||||
26 | |||||
27 | 1 | 3µs | 1 | 1.09ms | $loader->(); # spent 1.09ms making 1 call to Module::Implementation::__ANON__[Module/Implementation.pm:52] |
28 | } | ||||
29 | |||||
30 | 1 | 900ns | our @EXPORT_OK = qw/load_class load_optional_class try_load_class is_class_loaded load_first_existing_class/; | ||
31 | 1 | 1µs | our %EXPORT_TAGS = ( | ||
32 | all => \@EXPORT_OK, | ||||
33 | ); | ||||
34 | |||||
35 | 1 | 100ns | our $ERROR; | ||
36 | |||||
37 | sub load_class { | ||||
38 | my $class = shift; | ||||
39 | my $options = shift; | ||||
40 | |||||
41 | my ($res, $e) = try_load_class($class, $options); | ||||
42 | return $class if $res; | ||||
43 | |||||
44 | _croak($e); | ||||
45 | } | ||||
46 | |||||
47 | sub load_first_existing_class { | ||||
48 | my $classes = Data::OptList::mkopt(\@_) | ||||
49 | or return; | ||||
50 | |||||
51 | foreach my $class (@{$classes}) { | ||||
52 | check_module_name($class->[0]); | ||||
53 | } | ||||
54 | |||||
55 | for my $class (@{$classes}) { | ||||
56 | my ($name, $options) = @{$class}; | ||||
57 | |||||
58 | # We need to be careful not to pass an undef $options to this sub, | ||||
59 | # since the XS version will blow up if that happens. | ||||
60 | return $name if is_class_loaded($name, ($options ? $options : ())); | ||||
61 | |||||
62 | my ($res, $e) = try_load_class($name, $options); | ||||
63 | |||||
64 | return $name if $res; | ||||
65 | |||||
66 | my $file = module_notional_filename($name); | ||||
67 | |||||
68 | next if $e =~ /^Can't locate \Q$file\E in \@INC/; | ||||
69 | next | ||||
70 | if $options | ||||
71 | && defined $options->{-version} | ||||
72 | && $e =~ _version_fail_re($name, $options->{-version}); | ||||
73 | |||||
74 | _croak("Couldn't load class ($name) because: $e"); | ||||
75 | } | ||||
76 | |||||
77 | my @list = map { | ||||
78 | $_->[0] | ||||
79 | . ( $_->[1] && defined $_->[1]{-version} | ||||
80 | ? " (version >= $_->[1]{-version})" | ||||
81 | : q{} ) | ||||
82 | } @{$classes}; | ||||
83 | |||||
84 | my $err | ||||
85 | .= q{Can't locate } | ||||
86 | . _or_list(@list) | ||||
87 | . " in \@INC (\@INC contains: @INC)."; | ||||
88 | _croak($err); | ||||
89 | } | ||||
90 | |||||
91 | sub _version_fail_re { | ||||
92 | my $name = shift; | ||||
93 | my $vers = shift; | ||||
94 | |||||
95 | return qr/\Q$name\E version \Q$vers\E required--this is only version/; | ||||
96 | } | ||||
97 | |||||
98 | sub _nonexistent_fail_re { | ||||
99 | my $name = shift; | ||||
100 | |||||
101 | my $file = module_notional_filename($name); | ||||
102 | return qr/Can't locate \Q$file\E in \@INC/; | ||||
103 | } | ||||
104 | |||||
105 | sub _or_list { | ||||
106 | return $_[0] if @_ == 1; | ||||
107 | |||||
108 | return join ' or ', @_ if @_ ==2; | ||||
109 | |||||
110 | my $last = pop; | ||||
111 | |||||
112 | my $list = join ', ', @_; | ||||
113 | $list .= ', or ' . $last; | ||||
114 | |||||
115 | return $list; | ||||
116 | } | ||||
117 | |||||
118 | sub load_optional_class { | ||||
119 | my $class = shift; | ||||
120 | my $options = shift; | ||||
121 | |||||
122 | check_module_name($class); | ||||
123 | |||||
124 | my ($res, $e) = try_load_class($class, $options); | ||||
125 | return 1 if $res; | ||||
126 | |||||
127 | return 0 | ||||
128 | if $options | ||||
129 | && defined $options->{-version} | ||||
130 | && $e =~ _version_fail_re($class, $options->{-version}); | ||||
131 | |||||
132 | return 0 | ||||
133 | if $e =~ _nonexistent_fail_re($class); | ||||
134 | |||||
135 | _croak($e); | ||||
136 | } | ||||
137 | |||||
138 | sub try_load_class { | ||||
139 | my $class = shift; | ||||
140 | my $options = shift; | ||||
141 | |||||
142 | check_module_name($class); | ||||
143 | |||||
144 | local $@; | ||||
145 | undef $ERROR; | ||||
146 | |||||
147 | if (is_class_loaded($class)) { | ||||
148 | # We need to check this here rather than in is_class_loaded() because | ||||
149 | # we want to return the error message for a failed version check, but | ||||
150 | # is_class_loaded just returns true/false. | ||||
151 | return 1 unless $options && defined $options->{-version}; | ||||
152 | return try { | ||||
153 | $class->VERSION($options->{-version}); | ||||
154 | 1; | ||||
155 | } | ||||
156 | catch { | ||||
157 | _error($_); | ||||
158 | }; | ||||
159 | } | ||||
160 | |||||
161 | my $file = module_notional_filename($class); | ||||
162 | # This says "our diagnostics of the package | ||||
163 | # say perl's INC status about the file being loaded are | ||||
164 | # wrong", so we delete it from %INC, so when we call require(), | ||||
165 | # perl will *actually* try reloading the file. | ||||
166 | # | ||||
167 | # If the file is already in %INC, it won't retry, | ||||
168 | # And on 5.8, it won't fail either! | ||||
169 | # | ||||
170 | # The extra benefit of this trick, is it helps even on | ||||
171 | # 5.10, as instead of dying with "Compilation failed", | ||||
172 | # it will die with the actual error, and that's a win-win. | ||||
173 | delete $INC{$file}; | ||||
174 | return try { | ||||
175 | local $SIG{__DIE__} = 'DEFAULT'; | ||||
176 | if ($options && defined $options->{-version}) { | ||||
177 | use_module($class, $options->{-version}); | ||||
178 | } | ||||
179 | else { | ||||
180 | require_module($class); | ||||
181 | } | ||||
182 | 1; | ||||
183 | } | ||||
184 | catch { | ||||
185 | _error($_); | ||||
186 | }; | ||||
187 | } | ||||
188 | |||||
189 | sub _error { | ||||
190 | my $e = shift; | ||||
191 | |||||
192 | $e =~ s/ at .+?Runtime\.pm line [0-9]+\.$//; | ||||
193 | chomp $e; | ||||
194 | |||||
195 | $ERROR = $e; | ||||
196 | return 0 unless wantarray; | ||||
197 | return 0, $ERROR; | ||||
198 | } | ||||
199 | |||||
200 | sub _croak { | ||||
201 | require Carp; | ||||
202 | local $Carp::CarpLevel = $Carp::CarpLevel + 2; | ||||
203 | Carp::croak(shift); | ||||
204 | } | ||||
205 | |||||
206 | 1 | 4µs | 1; | ||
207 | |||||
208 | 1 | 13µs | 1 | 246µs | __END__ # spent 246µs making 1 call to B::Hooks::EndOfScope::XS::__ANON__[B/Hooks/EndOfScope/XS.pm:26] |