Filename | /usr/local/perls/perl-5.26.1/lib/5.26.1/XSLoader.pm |
Statements | Executed 0 statements in 0s |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
4 | 4 | 4 | 10.8ms | 10.8ms | load | XSLoader::
0 | 0 | 0 | 0s | 0s | bootstrap_inherit | XSLoader::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # Generated from XSLoader_pm.PL (resolved %Config::Config value) | ||||
2 | # This file is unique for every OS | ||||
3 | |||||
4 | package XSLoader; | ||||
5 | |||||
6 | $VERSION = "0.27"; | ||||
7 | |||||
8 | #use strict; | ||||
9 | |||||
10 | package DynaLoader; | ||||
11 | |||||
12 | # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here. | ||||
13 | # NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB | ||||
14 | boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) && | ||||
15 | !defined(&dl_error); | ||||
16 | package XSLoader; | ||||
17 | |||||
18 | # spent 10.8ms within XSLoader::load which was called 4 times, avg 2.69ms/call:
# once (5.32ms+0s) by IO::Uncompress::Adapter::Bunzip2::BEGIN@9 at line 62 of Compress/Raw/Bzip2.pm
# once (2.10ms+0s) by Compress::Zlib::BEGIN@11 at line 126 of Compress/Raw/Zlib.pm
# once (1.70ms+0s) by File::GlobMapper::BEGIN@10 at line 68 of File/Glob.pm
# once (1.64ms+0s) by CPAN::has_inst at line 80 of Storable.pm | ||||
19 | package DynaLoader; | ||||
20 | |||||
21 | my ($caller, $modlibname) = caller(); | ||||
22 | my $module = $caller; | ||||
23 | |||||
24 | if (@_) { | ||||
25 | $module = $_[0]; | ||||
26 | } else { | ||||
27 | $_[0] = $module; | ||||
28 | } | ||||
29 | |||||
30 | # work with static linking too | ||||
31 | my $boots = "$module\::bootstrap"; | ||||
32 | goto &$boots if defined &$boots; | ||||
33 | |||||
34 | goto \&XSLoader::bootstrap_inherit unless $module and defined &dl_load_file; | ||||
35 | |||||
36 | my @modparts = split(/::/,$module); | ||||
37 | my $modfname = $modparts[-1]; | ||||
38 | |||||
39 | my $modpname = join('/',@modparts); | ||||
40 | my $c = () = split(/::/,$caller,-1); | ||||
41 | $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename | ||||
42 | # Does this look like a relative path? | ||||
43 | if ($modlibname !~ m{^/}) { | ||||
44 | # Someone may have a #line directive that changes the file name, or | ||||
45 | # may be calling XSLoader::load from inside a string eval. We cer- | ||||
46 | # tainly do not want to go loading some code that is not in @INC, | ||||
47 | # as it could be untrusted. | ||||
48 | # | ||||
49 | # We could just fall back to DynaLoader here, but then the rest of | ||||
50 | # this function would go untested in the perl core, since all @INC | ||||
51 | # paths are relative during testing. That would be a time bomb | ||||
52 | # waiting to happen, since bugs could be introduced into the code. | ||||
53 | # | ||||
54 | # So look through @INC to see if $modlibname is in it. A rela- | ||||
55 | # tive $modlibname is not a common occurrence, so this block is | ||||
56 | # not hot code. | ||||
57 | FOUND: { | ||||
58 | for (@INC) { | ||||
59 | if ($_ eq $modlibname) { | ||||
60 | last FOUND; | ||||
61 | } | ||||
62 | } | ||||
63 | # Not found. Fall back to DynaLoader. | ||||
64 | goto \&XSLoader::bootstrap_inherit; | ||||
65 | } | ||||
66 | } | ||||
67 | my $file = "$modlibname/auto/$modpname/$modfname.bundle"; | ||||
68 | |||||
69 | # print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug; | ||||
70 | |||||
71 | my $bs = $file; | ||||
72 | $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library | ||||
73 | |||||
74 | if (-s $bs) { # only read file if it's not empty | ||||
75 | # print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug; | ||||
76 | eval { local @INC = ('.'); do $bs; }; | ||||
77 | warn "$bs: $@\n" if $@; | ||||
78 | goto \&XSLoader::bootstrap_inherit; | ||||
79 | } | ||||
80 | |||||
81 | goto \&XSLoader::bootstrap_inherit if not -f $file; | ||||
82 | |||||
83 | my $bootname = "boot_$module"; | ||||
84 | $bootname =~ s/\W/_/g; | ||||
85 | @DynaLoader::dl_require_symbols = ($bootname); | ||||
86 | |||||
87 | my $boot_symbol_ref; | ||||
88 | |||||
89 | if ($boot_symbol_ref = dl_find_symbol( 0, $bootname )) { | ||||
90 | goto boot; #extension library has already been loaded, e.g. darwin | ||||
91 | } | ||||
92 | # Many dynamic extension loading problems will appear to come from | ||||
93 | # this section of code: XYZ failed at line 123 of DynaLoader.pm. | ||||
94 | # Often these errors are actually occurring in the initialisation | ||||
95 | # C code of the extension XS file. Perl reports the error as being | ||||
96 | # in this perl code simply because this was the last perl code | ||||
97 | # it executed. | ||||
98 | |||||
99 | my $libref = dl_load_file($file, 0) or do { | ||||
100 | require Carp; | ||||
101 | Carp::croak("Can't load '$file' for module $module: " . dl_error()); | ||||
102 | }; | ||||
103 | push(@DynaLoader::dl_librefs,$libref); # record loaded object | ||||
104 | |||||
105 | $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do { | ||||
106 | require Carp; | ||||
107 | Carp::croak("Can't find '$bootname' symbol in $file\n"); | ||||
108 | }; | ||||
109 | |||||
110 | push(@DynaLoader::dl_modules, $module); # record loaded module | ||||
111 | |||||
112 | boot: | ||||
113 | my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file); | ||||
114 | |||||
115 | # See comment block above | ||||
116 | push(@DynaLoader::dl_shared_objects, $file); # record files loaded | ||||
117 | return &$xs(@_); | ||||
118 | } | ||||
119 | |||||
120 | sub bootstrap_inherit { | ||||
121 | require DynaLoader; | ||||
122 | goto \&DynaLoader::bootstrap_inherit; | ||||
123 | } | ||||
124 | |||||
125 | 1; | ||||
126 | |||||
127 | |||||
128 | __END__ | ||||
129 | |||||
130 | =head1 NAME | ||||
131 | |||||
132 | XSLoader - Dynamically load C libraries into Perl code | ||||
133 | |||||
134 | =head1 VERSION | ||||
135 | |||||
136 | Version 0.24 | ||||
137 | |||||
138 | =head1 SYNOPSIS | ||||
139 | |||||
140 | package YourPackage; | ||||
141 | require XSLoader; | ||||
142 | |||||
143 | XSLoader::load(); | ||||
144 | |||||
145 | =head1 DESCRIPTION | ||||
146 | |||||
147 | This module defines a standard I<simplified> interface to the dynamic | ||||
148 | linking mechanisms available on many platforms. Its primary purpose is | ||||
149 | to implement cheap automatic dynamic loading of Perl modules. | ||||
150 | |||||
151 | For a more complicated interface, see L<DynaLoader>. Many (most) | ||||
152 | features of C<DynaLoader> are not implemented in C<XSLoader>, like for | ||||
153 | example the C<dl_load_flags>, not honored by C<XSLoader>. | ||||
154 | |||||
155 | =head2 Migration from C<DynaLoader> | ||||
156 | |||||
157 | A typical module using L<DynaLoader|DynaLoader> starts like this: | ||||
158 | |||||
159 | package YourPackage; | ||||
160 | require DynaLoader; | ||||
161 | |||||
162 | our @ISA = qw( OnePackage OtherPackage DynaLoader ); | ||||
163 | our $VERSION = '0.01'; | ||||
164 | bootstrap YourPackage $VERSION; | ||||
165 | |||||
166 | Change this to | ||||
167 | |||||
168 | package YourPackage; | ||||
169 | use XSLoader; | ||||
170 | |||||
171 | our @ISA = qw( OnePackage OtherPackage ); | ||||
172 | our $VERSION = '0.01'; | ||||
173 | XSLoader::load 'YourPackage', $VERSION; | ||||
174 | |||||
175 | In other words: replace C<require DynaLoader> by C<use XSLoader>, remove | ||||
176 | C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not | ||||
177 | forget to quote the name of your package on the C<XSLoader::load> line, | ||||
178 | and add comma (C<,>) before the arguments (C<$VERSION> above). | ||||
179 | |||||
180 | Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have | ||||
181 | the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the | ||||
182 | more backward-compatible | ||||
183 | |||||
184 | use vars qw($VERSION @ISA); | ||||
185 | |||||
186 | one can remove this reference to C<@ISA> together with the C<@ISA> assignment. | ||||
187 | |||||
188 | If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes | ||||
189 | |||||
190 | XSLoader::load 'YourPackage'; | ||||
191 | |||||
192 | If the call to C<load> is from C<YourPackage>, then that can be further | ||||
193 | simplified to | ||||
194 | |||||
195 | XSLoader::load(); | ||||
196 | |||||
197 | as C<load> will use C<caller> to determine the package. | ||||
198 | |||||
199 | =head2 Backward compatible boilerplate | ||||
200 | |||||
201 | If you want to have your cake and eat it too, you need a more complicated | ||||
202 | boilerplate. | ||||
203 | |||||
204 | package YourPackage; | ||||
205 | use vars qw($VERSION @ISA); | ||||
206 | |||||
207 | @ISA = qw( OnePackage OtherPackage ); | ||||
208 | $VERSION = '0.01'; | ||||
209 | eval { | ||||
210 | require XSLoader; | ||||
211 | XSLoader::load('YourPackage', $VERSION); | ||||
212 | 1; | ||||
213 | } or do { | ||||
214 | require DynaLoader; | ||||
215 | push @ISA, 'DynaLoader'; | ||||
216 | bootstrap YourPackage $VERSION; | ||||
217 | }; | ||||
218 | |||||
219 | The parentheses about C<XSLoader::load()> arguments are needed since we replaced | ||||
220 | C<use XSLoader> by C<require>, so the compiler does not know that a function | ||||
221 | C<XSLoader::load()> is present. | ||||
222 | |||||
223 | This boilerplate uses the low-overhead C<XSLoader> if present; if used with | ||||
224 | an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>. | ||||
225 | |||||
226 | =head1 Order of initialization: early load() | ||||
227 | |||||
228 | I<Skip this section if the XSUB functions are supposed to be called from other | ||||
229 | modules only; read it only if you call your XSUBs from the code in your module, | ||||
230 | or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">). | ||||
231 | What is described here is equally applicable to the L<DynaLoader|DynaLoader> | ||||
232 | interface.> | ||||
233 | |||||
234 | A sufficiently complicated module using XS would have both Perl code (defined | ||||
235 | in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this | ||||
236 | Perl code makes calls into this XS code, and/or this XS code makes calls to | ||||
237 | the Perl code, one should be careful with the order of initialization. | ||||
238 | |||||
239 | The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's | ||||
240 | bootstrap code. For modules build by F<xsubpp> (nearly all modules) this | ||||
241 | has three side effects: | ||||
242 | |||||
243 | =over | ||||
244 | |||||
245 | =item * | ||||
246 | |||||
247 | A sanity check is done to ensure that the versions of the F<.pm> and the | ||||
248 | (compiled) F<.xs> parts are compatible. If C<$VERSION> was specified, this | ||||
249 | is used for the check. If not specified, it defaults to | ||||
250 | C<$XS_VERSION // $VERSION> (in the module's namespace) | ||||
251 | |||||
252 | =item * | ||||
253 | |||||
254 | the XSUBs are made accessible from Perl | ||||
255 | |||||
256 | =item * | ||||
257 | |||||
258 | if a C<BOOT:> section was present in the F<.xs> file, the code there is called. | ||||
259 | |||||
260 | =back | ||||
261 | |||||
262 | Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is | ||||
263 | convenient to have XSUBs installed before the Perl code is defined; for | ||||
264 | example, this makes prototypes for XSUBs visible to this Perl code. | ||||
265 | Alternatively, if the C<BOOT:> section makes calls to Perl functions (or | ||||
266 | uses Perl variables) defined in the F<.pm> file, they must be defined prior to | ||||
267 | the call to C<XSLoader::load()> (or C<bootstrap()>). | ||||
268 | |||||
269 | The first situation being much more frequent, it makes sense to rewrite the | ||||
270 | boilerplate as | ||||
271 | |||||
272 | package YourPackage; | ||||
273 | use XSLoader; | ||||
274 | use vars qw($VERSION @ISA); | ||||
275 | |||||
276 | BEGIN { | ||||
277 | @ISA = qw( OnePackage OtherPackage ); | ||||
278 | $VERSION = '0.01'; | ||||
279 | |||||
280 | # Put Perl code used in the BOOT: section here | ||||
281 | |||||
282 | XSLoader::load 'YourPackage', $VERSION; | ||||
283 | } | ||||
284 | |||||
285 | # Put Perl code making calls into XSUBs here | ||||
286 | |||||
287 | =head2 The most hairy case | ||||
288 | |||||
289 | If the interdependence of your C<BOOT:> section and Perl code is | ||||
290 | more complicated than this (e.g., the C<BOOT:> section makes calls to Perl | ||||
291 | functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:> | ||||
292 | section altogether. Replace it with a function C<onBOOT()>, and call it like | ||||
293 | this: | ||||
294 | |||||
295 | package YourPackage; | ||||
296 | use XSLoader; | ||||
297 | use vars qw($VERSION @ISA); | ||||
298 | |||||
299 | BEGIN { | ||||
300 | @ISA = qw( OnePackage OtherPackage ); | ||||
301 | $VERSION = '0.01'; | ||||
302 | XSLoader::load 'YourPackage', $VERSION; | ||||
303 | } | ||||
304 | |||||
305 | # Put Perl code used in onBOOT() function here; calls to XSUBs are | ||||
306 | # prototype-checked. | ||||
307 | |||||
308 | onBOOT; | ||||
309 | |||||
310 | # Put Perl initialization code assuming that XS is initialized here | ||||
311 | |||||
312 | |||||
313 | =head1 DIAGNOSTICS | ||||
314 | |||||
315 | =over | ||||
316 | |||||
317 | =item C<Can't find '%s' symbol in %s> | ||||
318 | |||||
319 | B<(F)> The bootstrap symbol could not be found in the extension module. | ||||
320 | |||||
321 | =item C<Can't load '%s' for module %s: %s> | ||||
322 | |||||
323 | B<(F)> The loading or initialisation of the extension module failed. | ||||
324 | The detailed error follows. | ||||
325 | |||||
326 | =item C<Undefined symbols present after loading %s: %s> | ||||
327 | |||||
328 | B<(W)> As the message says, some symbols stay undefined although the | ||||
329 | extension module was correctly loaded and initialised. The list of undefined | ||||
330 | symbols follows. | ||||
331 | |||||
332 | =back | ||||
333 | |||||
334 | =head1 LIMITATIONS | ||||
335 | |||||
336 | To reduce the overhead as much as possible, only one possible location | ||||
337 | is checked to find the extension DLL (this location is where C<make install> | ||||
338 | would put the DLL). If not found, the search for the DLL is transparently | ||||
339 | delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list. | ||||
340 | |||||
341 | In particular, this is applicable to the structure of C<@INC> used for testing | ||||
342 | not-yet-installed extensions. This means that running uninstalled extensions | ||||
343 | may have much more overhead than running the same extensions after | ||||
344 | C<make install>. | ||||
345 | |||||
346 | |||||
347 | =head1 KNOWN BUGS | ||||
348 | |||||
349 | The new simpler way to call C<XSLoader::load()> with no arguments at all | ||||
350 | does not work on Perl 5.8.4 and 5.8.5. | ||||
351 | |||||
352 | |||||
353 | =head1 BUGS | ||||
354 | |||||
355 | Please report any bugs or feature requests via the perlbug(1) utility. | ||||
356 | |||||
357 | |||||
358 | =head1 SEE ALSO | ||||
359 | |||||
360 | L<DynaLoader> | ||||
361 | |||||
362 | |||||
363 | =head1 AUTHORS | ||||
364 | |||||
365 | Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>. | ||||
366 | |||||
367 | CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni | ||||
368 | E<lt>[email protected]<gt>. | ||||
369 | |||||
370 | Previous maintainer was Michael G Schwern <[email protected]>. | ||||
371 | |||||
372 | |||||
373 | =head1 COPYRIGHT & LICENSE | ||||
374 | |||||
375 | Copyright (C) 1990-2011 by Larry Wall and others. | ||||
376 | |||||
377 | This program is free software; you can redistribute it and/or modify | ||||
378 | it under the same terms as Perl itself. | ||||
379 | |||||
380 | =cut |