1N/A# $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug; 1N/A# No prizes for guessing why we don't say 'bootstrap DynaLoader;' here. 1N/A# NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB 1N/A # work with static linking too 1N/A my $b =
"$module\::bootstrap";
1N/A goto &$b
if defined &$b;
1N/A # Some systems have restrictions on files names for DLL's etc. 1N/A # mod2fname returns appropriate file base name (typically truncated) 1N/A # It may also edit @modparts if required. 1N/A# print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug; 1N/A $
bs =~ s/(\.\w+)?(;\d*)?$/\.
bs/;
# look for .bs 'beside' the library 1N/A if ($^O
eq 'darwin') {
1N/A goto boot;
#extension library has already been loaded, e.g. darwin 1N/A # Many dynamic extension loading problems will appear to come from 1N/A # Often these errors are actually occurring in the initialisation 1N/A # C code of the extension XS file. Perl reports the error as being 1N/A # in this perl code simply because this was the last perl code 1N/A Carp::
carp(
"Undefined symbols present after loading $file: @unresolved\n");
1N/A Carp::
croak(
"Can't find '$bootname' symbol in $file\n");
1N/A # See comment block above 1N/AXSLoader - Dynamically load C libraries into Perl code 1N/A package YourPackage; 1N/A XSLoader::load 'YourPackage', $YourPackage::VERSION; 1N/AThis module defines a standard I<simplified> interface to the dynamic 1N/Alinking mechanisms available on many platforms. Its primary purpose is 1N/Ato implement cheap automatic dynamic loading of Perl modules. 1N/AFor more complicated interface see L<DynaLoader>. Many (most) 1N/Afeatures of DynaLoader are not implemented in XSLoader, like for 1N/Aexample the dl_load_flags is not honored by XSLoader. 1N/A=head2 Migration from C<DynaLoader> 1N/AA typical module using L<DynaLoader|DynaLoader> starts like this: 1N/A package YourPackage; 1N/A our @ISA = qw( OnePackage OtherPackage DynaLoader ); 1N/A our $VERSION = '0.01'; 1N/A bootstrap YourPackage $VERSION; 1N/A package YourPackage; 1N/A our @ISA = qw( OnePackage OtherPackage ); 1N/A our $VERSION = '0.01'; 1N/A XSLoader::load 'YourPackage', $VERSION; 1N/AIn other words: replace C<require DynaLoader> by C<use XSLoader>, remove 1N/AC<DynaLoader> from @ISA, change C<bootstrap> by C<XSLoader::load>. Do not 1N/Aforget to quote the name of your package on the C<XSLoader::load> line, 1N/Aand add comma (C<,>) before the arguments ($VERSION above). 1N/AOf course, if @ISA contained only C<DynaLoader>, there is no need to have the 1N/A@ISA assignment at all; moreover, if instead of C<our> one uses 1N/A use vars qw($VERSION @ISA); 1N/Aone can remove this reference to @ISA together with the @ISA assignment 1N/AIf no $VERSION was specified on the C<bootstrap> line, the last line becomes 1N/A XSLoader::load 'YourPackage'; 1N/A=head2 Backward compatible boilerplate 1N/AIf you want to have your cake and eat it too, you need a more complicated 1N/A package YourPackage; 1N/A use vars qw($VERSION @ISA); 1N/A @ISA = qw( OnePackage OtherPackage ); 1N/A XSLoader::load('YourPackage', $VERSION); 1N/A push @ISA, 'DynaLoader'; 1N/A bootstrap YourPackage $VERSION; 1N/AThe parentheses about XSLoader::load() arguments are needed since we replaced 1N/AC<use XSLoader> by C<require>, so the compiler does not know that a function 1N/AXSLoader::load() is present. 1N/AThis boilerplate uses the low-overhead C<XSLoader> if present; if used with 1N/Aan antic Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>. 1N/A=head1 Order of initialization: early load() 1N/AI<Skip this section if the XSUB functions are supposed to be called from other 1N/Amodules only; read it only if you call your XSUBs from the code in your module, 1N/Aor have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">). 1N/AWhat is described here is equally applicable to L<DynaLoader|DynaLoader> 1N/AA sufficiently complicated module using XS would have both Perl code (defined 1N/APerl code makes calls into this XS code, and/or this XS code makes calls to 1N/Athe Perl code, one should be careful with the order of initialization. 1N/AThe call to XSLoader::load() (or bootstrap()) has three side effects: 1N/Aif $VERSION was specified, a sanity check is done to insure that the versions 1N/Aof the F<.pm> and the (compiled) F<.xs> parts are compatible; 1N/AThe XSUBs are made accessible from Perl; 1N/AIf the C<BOOT:> section was present in F<.xs> file, the code there is called. 1N/AConsequently, if the code in F<.pm> file makes calls to these XSUBs, it is 1N/Aconvenient to have XSUBs installed before the Perl code is defined; for 1N/Aexample, this makes prototypes for XSUBs visible to this Perl code. 1N/AAlternatively, if the C<BOOT:> section makes calls to Perl functions (or 1N/Auses Perl variables) defined in F<.pm> file, they must be defined prior to 1N/Athe call to XSLoader::load() (or bootstrap()). 1N/AThe first situation being much more frequent, it makes sense to rewrite the 1N/A package YourPackage; 1N/A use vars qw($VERSION @ISA); 1N/A @ISA = qw( OnePackage OtherPackage ); 1N/A # Put Perl code used in the BOOT: section here 1N/A XSLoader::load 'YourPackage', $VERSION; 1N/A # Put Perl code making calls into XSUBs here 1N/A=head2 The most hairy case 1N/AIf the interdependence of your C<BOOT:> section and Perl code is 1N/Amore complicated than this (e.g., the C<BOOT:> section makes calls to Perl 1N/Afunctions which make calls to XSUBs with prototypes), get rid of the C<BOOT:> 1N/Asection altogether. Replace it with a function onBOOT(), and call it like 1N/A package YourPackage; 1N/A use vars qw($VERSION @ISA); 1N/A @ISA = qw( OnePackage OtherPackage ); 1N/A XSLoader::load 'YourPackage', $VERSION; 1N/A # Put Perl code used in onBOOT() function here; calls to XSUBs are 1N/A # prototype-checked. 1N/A # Put Perl initialization code assuming that XS is initialized here 1N/ATo reduce the overhead as much as possible, only one possible location 1N/Ais checked to find the extension DLL (this location is where C<make install> 1N/Awould put the DLL). If not found, the search for the DLL is transparently 1N/Adelegated to C<DynaLoader>, which looks for the DLL along the @INC list. 1N/AIn particular, this is applicable to the structure of @INC used for testing 1N/Anot-yet-installed extensions. This means that the overhead of running 1N/Auninstalled extension may be much more than running the same extension after 1N/AIlya Zakharevich: extraction from DynaLoader.