4 1 while unlink "XSLoader.pm";
5 open OUT, ">XSLoader.pm" or die $!;
7 # Generated from XSLoader.pm.PL (resolved %Config::Config value)
19 # dlutils.c before 5.006 has this:
22 # dl_debug = SvIV( perl_get_sv("DynaLoader::dl_debug", 0x04) );
25 # where 0x04 is GV_ADDWARN, which causes a warning to be issued by the call
26 # into XS below, if DynaLoader.pm hasn't been loaded.
27 # It was changed to 0 in the commit(s) that added XSLoader to the core
28 # (9cf41c4d23a47c8b and its parent 9426adcd48655815)
29 # Hence to backport XSLoader to work silently with earlier DynaLoaders we need
30 # to ensure that the variable exists:
32 print OUT <<'EOT' if $] < 5.006;
34 # enable debug/trace messages from DynaLoader perl code
35 $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
40 # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
41 # NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
42 boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
49 my ($module, $modlibname) = caller();
57 # work with static linking too
58 my $boots = "$module\::bootstrap";
59 goto &$boots if defined &$boots;
61 goto \&XSLoader::bootstrap_inherit unless $module and defined &dl_load_file;
63 my @modparts = split(/::/,$module);
64 my $modfname = $modparts[-1];
68 print OUT <<'EOT' if defined &DynaLoader::mod2fname;
69 # Some systems have restrictions on files names for DLL's etc.
70 # mod2fname returns appropriate file base name (typically truncated)
71 # It may also edit @modparts if required.
72 $modfname = &mod2fname(\@modparts) if defined &mod2fname;
76 print OUT <<'EOT' if $^O eq 'os2';
78 # os2 static build can dynaload, but cannot dynaload Perl modules...
79 die 'Dynaloaded Perl modules are not available in this build of Perl' if $OS2::is_static;
84 my $modpname = join('/',@modparts);
86 $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
89 my $dl_dlext = quotemeta($Config::Config{'dlext'});
92 my \$file = "\$modlibname/auto/\$modpname/\$modfname.$dl_dlext";
97 # print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
100 $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
102 if (-s $bs) { # only read file if it's not empty
103 # print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
105 warn "$bs: $@\n" if $@;
108 goto \&XSLoader::bootstrap_inherit if not -f $file or -s $bs;
110 my $bootname = "boot_$module";
111 $bootname =~ s/\W/_/g;
112 @DynaLoader::dl_require_symbols = ($bootname);
118 if ($^O eq 'darwin') {
120 if ($boot_symbol_ref = dl_find_symbol(0, $bootname)) {
121 goto boot; #extension library has already been loaded, e.g. darwin
127 # Many dynamic extension loading problems will appear to come from
128 # this section of code: XYZ failed at line 123 of DynaLoader.pm.
129 # Often these errors are actually occurring in the initialisation
130 # C code of the extension XS file. Perl reports the error as being
131 # in this perl code simply because this was the last perl code
134 my $libref = dl_load_file($file, 0) or do {
136 Carp::croak("Can't load '$file' for module $module: " . dl_error());
138 push(@DynaLoader::dl_librefs,$libref); # record loaded object
140 my @unresolved = dl_undef_symbols();
143 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
146 $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
148 Carp::croak("Can't find '$bootname' symbol in $file\n");
151 push(@DynaLoader::dl_modules, $module); # record loaded module
154 my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file);
156 # See comment block above
157 push(@DynaLoader::dl_shared_objects, $file); # record files loaded
162 # Can't test with DynaLoader->can('bootstrap_inherit') when building in the
163 # core, as XSLoader gets built before DynaLoader.
168 sub bootstrap_inherit {
170 goto \&DynaLoader::bootstrap_inherit;
177 sub bootstrap_inherit {
178 # Versions of DynaLoader prior to 5.6.0 don't have bootstrap_inherit.
182 local *DynaLoader::isa = *{"$module\::ISA"};
183 local @DynaLoader::isa = (@DynaLoader::isa, 'DynaLoader');
184 # Cannot goto due to delocalization. Will report errors on a wrong line?
186 DynaLoader::bootstrap(@_);
200 XSLoader - Dynamically load C libraries into Perl code
215 This module defines a standard I<simplified> interface to the dynamic
216 linking mechanisms available on many platforms. Its primary purpose is
217 to implement cheap automatic dynamic loading of Perl modules.
219 For a more complicated interface, see L<DynaLoader>. Many (most)
220 features of C<DynaLoader> are not implemented in C<XSLoader>, like for
221 example the C<dl_load_flags>, not honored by C<XSLoader>.
223 =head2 Migration from C<DynaLoader>
225 A typical module using L<DynaLoader|DynaLoader> starts like this:
230 our @ISA = qw( OnePackage OtherPackage DynaLoader );
231 our $VERSION = '0.01';
232 bootstrap YourPackage $VERSION;
239 our @ISA = qw( OnePackage OtherPackage );
240 our $VERSION = '0.01';
241 XSLoader::load 'YourPackage', $VERSION;
243 In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
244 C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not
245 forget to quote the name of your package on the C<XSLoader::load> line,
246 and add comma (C<,>) before the arguments (C<$VERSION> above).
248 Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
249 the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
250 more backward-compatible
252 use vars qw($VERSION @ISA);
254 one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
256 If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
258 XSLoader::load 'YourPackage';
260 If the call to C<load> is from C<YourPackage>, then that can be further
265 as C<load> will use C<caller> to determine the package.
267 =head2 Backward compatible boilerplate
269 If you want to have your cake and eat it too, you need a more complicated
273 use vars qw($VERSION @ISA);
275 @ISA = qw( OnePackage OtherPackage );
279 XSLoader::load('YourPackage', $VERSION);
283 push @ISA, 'DynaLoader';
284 bootstrap YourPackage $VERSION;
287 The parentheses about C<XSLoader::load()> arguments are needed since we replaced
288 C<use XSLoader> by C<require>, so the compiler does not know that a function
289 C<XSLoader::load()> is present.
291 This boilerplate uses the low-overhead C<XSLoader> if present; if used with
292 an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
294 =head1 Order of initialization: early load()
296 I<Skip this section if the XSUB functions are supposed to be called from other
297 modules only; read it only if you call your XSUBs from the code in your module,
298 or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
299 What is described here is equally applicable to the L<DynaLoader|DynaLoader>
302 A sufficiently complicated module using XS would have both Perl code (defined
303 in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
304 Perl code makes calls into this XS code, and/or this XS code makes calls to
305 the Perl code, one should be careful with the order of initialization.
307 The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's
308 bootstrap code. For modules build by F<xsubpp> (nearly all modules) this
309 has three side effects:
315 A sanity check is done to ensure that the versions of the F<.pm> and the
316 (compiled) F<.xs> parts are compatible. If C<$VERSION> was specified, this
317 is used for the check. If not specified, it defaults to
318 C<$XS_VERSION // $VERSION> (in the module's namespace)
322 the XSUBs are made accessible from Perl
326 if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
330 Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
331 convenient to have XSUBs installed before the Perl code is defined; for
332 example, this makes prototypes for XSUBs visible to this Perl code.
333 Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
334 uses Perl variables) defined in the F<.pm> file, they must be defined prior to
335 the call to C<XSLoader::load()> (or C<bootstrap()>).
337 The first situation being much more frequent, it makes sense to rewrite the
342 use vars qw($VERSION @ISA);
345 @ISA = qw( OnePackage OtherPackage );
348 # Put Perl code used in the BOOT: section here
350 XSLoader::load 'YourPackage', $VERSION;
353 # Put Perl code making calls into XSUBs here
355 =head2 The most hairy case
357 If the interdependence of your C<BOOT:> section and Perl code is
358 more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
359 functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
360 section altogether. Replace it with a function C<onBOOT()>, and call it like
365 use vars qw($VERSION @ISA);
368 @ISA = qw( OnePackage OtherPackage );
370 XSLoader::load 'YourPackage', $VERSION;
373 # Put Perl code used in onBOOT() function here; calls to XSUBs are
378 # Put Perl initialization code assuming that XS is initialized here
385 =item C<Can't find '%s' symbol in %s>
387 B<(F)> The bootstrap symbol could not be found in the extension module.
389 =item C<Can't load '%s' for module %s: %s>
391 B<(F)> The loading or initialisation of the extension module failed.
392 The detailed error follows.
394 =item C<Undefined symbols present after loading %s: %s>
396 B<(W)> As the message says, some symbols stay undefined although the
397 extension module was correctly loaded and initialised. The list of undefined
404 To reduce the overhead as much as possible, only one possible location
405 is checked to find the extension DLL (this location is where C<make install>
406 would put the DLL). If not found, the search for the DLL is transparently
407 delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
409 In particular, this is applicable to the structure of C<@INC> used for testing
410 not-yet-installed extensions. This means that running uninstalled extensions
411 may have much more overhead than running the same extensions after
417 The new simpler way to call C<XSLoader::load()> with no arguments at all
418 does not work on Perl 5.8.4 and 5.8.5.
423 Please report any bugs or feature requests via the perlbug(1) utility.
433 Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
435 CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
436 E<lt>sebastien@aperghis.netE<gt>.
438 Previous maintainer was Michael G Schwern <schwern@pobox.com>.
441 =head1 COPYRIGHT & LICENSE
443 Copyright (C) 1990-2011 by Larry Wall and others.
445 This program is free software; you can redistribute it and/or modify
446 it under the same terms as Perl itself.