1N/Ause Config;
1N/A
1N/Asub to_string {
1N/A my ($value) = @_;
1N/A $value =~ s/\\/\\\\/g;
1N/A $value =~ s/'/\\'/g;
1N/A return "'$value'";
1N/A}
1N/A
1N/Aunlink "XSLoader.pm" if -f "XSLoader.pm";
1N/Aopen OUT, ">XSLoader.pm" or die $!;
1N/Aprint OUT <<'EOT';
1N/A# Generated from XSLoader.pm.PL (resolved %Config::Config value)
1N/A
1N/Apackage XSLoader;
1N/A
1N/A$VERSION = "0.02";
1N/A
1N/A# enable debug/trace messages from DynaLoader perl code
1N/A# $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
1N/A
1N/AEOT
1N/A
1N/Aprint OUT ' my $dl_dlext = ', to_string($Config::Config{'dlext'}), ";\n" ;
1N/A
1N/Aprint OUT <<'EOT';
1N/A
1N/Apackage DynaLoader;
1N/A
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/Aboot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
1N/A !defined(&dl_error);
1N/Apackage XSLoader;
1N/A
1N/Asub load {
1N/A package DynaLoader;
1N/A
1N/A die q{XSLoader::load('Your::Module', $Your::Module::VERSION)} unless @_;
1N/A
1N/A my($module) = $_[0];
1N/A
1N/A # work with static linking too
1N/A my $b = "$module\::bootstrap";
1N/A goto &$b if defined &$b;
1N/A
1N/A goto retry unless $module and defined &dl_load_file;
1N/A
1N/A my @modparts = split(/::/,$module);
1N/A my $modfname = $modparts[-1];
1N/A
1N/AEOT
1N/A
1N/Aprint OUT <<'EOT' if defined &DynaLoader::mod2fname;
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 $modfname = &mod2fname(\@modparts) if defined &mod2fname;
1N/A
1N/AEOT
1N/A
1N/Aprint OUT <<'EOT';
1N/A my $modpname = join('/',@modparts);
1N/A my $modlibname = (caller())[1];
1N/A my $c = @modparts;
1N/A $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
1N/A my $file = "$modlibname/auto/$modpname/$modfname.$dl_dlext";
1N/A
1N/A# print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
1N/A
1N/A my $bs = $file;
1N/A $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
1N/A
1N/A goto retry if not -f $file or -s $bs;
1N/A
1N/A my $bootname = "boot_$module";
1N/A $bootname =~ s/\W/_/g;
1N/A @dl_require_symbols = ($bootname);
1N/A
1N/A my $boot_symbol_ref;
1N/A
1N/A if ($^O eq 'darwin') {
1N/A if ($boot_symbol_ref = dl_find_symbol(0, $bootname)) {
1N/A goto boot; #extension library has already been loaded, e.g. darwin
1N/A }
1N/A }
1N/A
1N/A # Many dynamic extension loading problems will appear to come from
1N/A # this section of code: XYZ failed at line 123 of DynaLoader.pm.
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 # it executed.
1N/A
1N/A my $libref = dl_load_file($file, 0) or do {
1N/A require Carp;
1N/A Carp::croak("Can't load '$file' for module $module: " . dl_error());
1N/A };
1N/A push(@dl_librefs,$libref); # record loaded object
1N/A
1N/A my @unresolved = dl_undef_symbols();
1N/A if (@unresolved) {
1N/A require Carp;
1N/A Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
1N/A }
1N/A
1N/A $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
1N/A require Carp;
1N/A Carp::croak("Can't find '$bootname' symbol in $file\n");
1N/A };
1N/A
1N/A push(@dl_modules, $module); # record loaded module
1N/A
1N/A boot:
1N/A my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file);
1N/A
1N/A # See comment block above
1N/A return &$xs(@_);
1N/A
1N/A retry:
1N/A require DynaLoader;
1N/A goto &DynaLoader::bootstrap_inherit;
1N/A}
1N/A
1N/A1;
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AXSLoader - Dynamically load C libraries into Perl code
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A package YourPackage;
1N/A use XSLoader;
1N/A
1N/A XSLoader::load 'YourPackage', $YourPackage::VERSION;
1N/A
1N/A=head1 DESCRIPTION
1N/A
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/A
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
1N/A=head2 Migration from C<DynaLoader>
1N/A
1N/AA typical module using L<DynaLoader|DynaLoader> starts like this:
1N/A
1N/A package YourPackage;
1N/A require DynaLoader;
1N/A
1N/A our @ISA = qw( OnePackage OtherPackage DynaLoader );
1N/A our $VERSION = '0.01';
1N/A bootstrap YourPackage $VERSION;
1N/A
1N/AChange this to
1N/A
1N/A package YourPackage;
1N/A use XSLoader;
1N/A
1N/A our @ISA = qw( OnePackage OtherPackage );
1N/A our $VERSION = '0.01';
1N/A XSLoader::load 'YourPackage', $VERSION;
1N/A
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/A
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/Abackward-compatible
1N/A
1N/A use vars qw($VERSION @ISA);
1N/A
1N/Aone can remove this reference to @ISA together with the @ISA assignment
1N/A
1N/AIf no $VERSION was specified on the C<bootstrap> line, the last line becomes
1N/A
1N/A XSLoader::load 'YourPackage';
1N/A
1N/A=head2 Backward compatible boilerplate
1N/A
1N/AIf you want to have your cake and eat it too, you need a more complicated
1N/Aboilerplate.
1N/A
1N/A package YourPackage;
1N/A use vars qw($VERSION @ISA);
1N/A
1N/A @ISA = qw( OnePackage OtherPackage );
1N/A $VERSION = '0.01';
1N/A eval {
1N/A require XSLoader;
1N/A XSLoader::load('YourPackage', $VERSION);
1N/A 1;
1N/A } or do {
1N/A require DynaLoader;
1N/A push @ISA, 'DynaLoader';
1N/A bootstrap YourPackage $VERSION;
1N/A };
1N/A
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/A
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
1N/A=head1 Order of initialization: early load()
1N/A
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/Ainterface.>
1N/A
1N/AA sufficiently complicated module using XS would have both Perl code (defined
1N/Ain F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
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/A
1N/AThe call to XSLoader::load() (or bootstrap()) has three side effects:
1N/A
1N/A=over
1N/A
1N/A=item *
1N/A
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/A
1N/A=item *
1N/A
1N/AThe XSUBs are made accessible from Perl;
1N/A
1N/A=item *
1N/A
1N/AIf the C<BOOT:> section was present in F<.xs> file, the code there is called.
1N/A
1N/A=back
1N/A
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/A
1N/AThe first situation being much more frequent, it makes sense to rewrite the
1N/Aboilerplate as
1N/A
1N/A package YourPackage;
1N/A use XSLoader;
1N/A use vars qw($VERSION @ISA);
1N/A
1N/A BEGIN {
1N/A @ISA = qw( OnePackage OtherPackage );
1N/A $VERSION = '0.01';
1N/A
1N/A # Put Perl code used in the BOOT: section here
1N/A
1N/A XSLoader::load 'YourPackage', $VERSION;
1N/A }
1N/A
1N/A # Put Perl code making calls into XSUBs here
1N/A
1N/A=head2 The most hairy case
1N/A
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/Athis:
1N/A
1N/A package YourPackage;
1N/A use XSLoader;
1N/A use vars qw($VERSION @ISA);
1N/A
1N/A BEGIN {
1N/A @ISA = qw( OnePackage OtherPackage );
1N/A $VERSION = '0.01';
1N/A XSLoader::load 'YourPackage', $VERSION;
1N/A }
1N/A
1N/A # Put Perl code used in onBOOT() function here; calls to XSUBs are
1N/A # prototype-checked.
1N/A
1N/A onBOOT;
1N/A
1N/A # Put Perl initialization code assuming that XS is initialized here
1N/A
1N/A=head1 LIMITATIONS
1N/A
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/A
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/AC<make install>.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/AIlya Zakharevich: extraction from DynaLoader.
1N/A
1N/A=cut
1N/AEOT
1N/A
1N/Aclose OUT or die $!;
1N/A