XSLoader(3pm)
π XSLoader β the manual, but make it make sense
okay so bestie, this is the XSLoader(3pm) page, from the Perl Programmers Reference Guide, and we're gonna get through it together. It's giving "small module, big responsibilities" fr fr.
πΈ NAME πΈ
XSLoader β dynamically load C libraries into Perl code. that's it. that's the whole vibe. it's the little loader that could. π
β¨ VERSION β¨
Version 0.32. note it, love it, respect it. π
π¦ SYNOPSIS π¦
package YourPackage;
require XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);
that's the whole thing. three lines. a whole C library just shows up. iconic behavior honestly. π
π DESCRIPTION π
so this module defines a standard, simplified interface to the dynamic linking mechanisms that exist on a bunch of platforms. its main character purpose? cheap automatic dynamic loading of Perl modules. it's literally the low-overhead queen. β¨
if you want the complicated interface, go see DynaLoader. and ngl, many (most) features of "DynaLoader" are not implemented in "XSLoader" β like, for example, the "dl_load_flags" are not honored by "XSLoader". so don't come here expecting the deluxe package. π
π Migration from "DynaLoader" π
a typical module using DynaLoader starts like this:
package YourPackage;
require DynaLoader;
our @ISA = qw( OnePackage OtherPackage DynaLoader );
our $VERSION = '0.01';
__PACKAGE__->bootstrap($VERSION);
change this to:
package YourPackage;
use XSLoader;
our @ISA = qw( OnePackage OtherPackage );
our $VERSION = '0.01';
XSLoader::load(__PACKAGE__, $VERSION);
in other words, bestie: replace "require DynaLoader" by "use XSLoader", remove "DynaLoader" from @ISA, and change "bootstrap" to "XSLoader::load". π«Ά
do not forget to quote the name of your package on the "XSLoader::load" line, and add a comma (,), before the arguments ($VERSION up there). tiny details, huge consequences. π₯
and of course, if @ISA contained only "DynaLoader", there is no need to have the @ISA assignment at all. also, if instead of "our" you use the more backward-compatible
use vars qw($VERSION @ISA);
then you can remove this reference to @ISA together with the @ISA assignment. slay, less code. π
if no $VERSION was specified on the "bootstrap" line, the last line becomes
XSLoader::load(__PACKAGE__);
in which case it can be further simplified to
XSLoader::load();
because "load" will use "caller" to determine the package. it just knows. main character energy. π
π Backward compatible boilerplate π
if you want to have your cake and eat it too (valid), you need a more complicated boilerplate:
package YourPackage;
our @ISA = qw( OnePackage OtherPackage );
our $VERSION = '0.01';
eval {
require XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);
1;
} or do {
require DynaLoader;
push @ISA, 'DynaLoader';
__PACKAGE__->bootstrap($VERSION);
};
the parentheses around XSLoader::load() arguments are needed since we replaced "use XSLoader" by "require", so the compiler does not know that a function XSLoader::load() is present. the compiler is doing its best, okay π
this boilerplate uses the low-overhead "XSLoader" if present; if it's used with an antique Perl which has no "XSLoader", it falls back to using "DynaLoader". graceful. resilient. we love a fallback. π¦
β Order of initialization: early load() β
skip this section if the XSUB functions are supposed to be called from other modules only. read it only if you call your XSUBs from the code in your module, or you have a"BOOT:"section in your XS file (see"The BOOT: Keyword"in perlxs). what's described here is equally applicable to the DynaLoader interface.
a sufficiently complicated module using XS would have both Perl code (defined in YourPackage.pm) and XS code (defined in YourPackage.xs). if this Perl code makes calls into this XS code, and/or this XS code makes calls to the Perl code, you gotta be careful with the order of initialization. it's giving choreography. π§
the call to XSLoader::load() (or bootstrap()) calls the module's bootstrap code. for modules built by xsubpp (nearly all modules) this has three side effects:
- a sanity check is done to ensure that the versions of the
.pmand the (compiled).xsparts are compatible. if$VERSIONwas specified, this is used for the check. if not specified, it defaults to"$XS_VERSION // $VERSION"(in the module's namespace) π - the XSUBs are made accessible from Perl
- if a
"BOOT:"section was present in the.xsfile, the code there is called.
consequently, if the code in the .pm file makes calls to these XSUBs, it's convenient to have XSUBs installed before the Perl code is defined β for example, this makes prototypes for XSUBs visible to this Perl code. alternatively, if the "BOOT:" section makes calls to Perl functions (or uses Perl variables) defined in the .pm file, they must be defined prior to the call to XSLoader::load() (or bootstrap()). order matters, no cap. π
the first situation being much more frequent, it makes sense to rewrite the boilerplate as:
package YourPackage;
use XSLoader;
our ($VERSION, @ISA);
BEGIN {
@ISA = qw( OnePackage OtherPackage );
$VERSION = '0.01';
# Put Perl code used in the BOOT: section here
XSLoader::load(__PACKAGE__, $VERSION);
}
# Put Perl code making calls into XSUBs here
π The most hairy case π
if the interdependence of your "BOOT:" section and Perl code is more complicated than this (e.g., the "BOOT:" section makes calls to Perl functions which make calls to XSUBs with prototypes), get rid of the "BOOT:" section altogether. yeet it. replace it with a function onBOOT(), and call it like this:
package YourPackage;
use XSLoader;
our ($VERSION, @ISA);
BEGIN {
@ISA = qw( OnePackage OtherPackage );
$VERSION = '0.01';
XSLoader::load(__PACKAGE__, $VERSION);
}
# Put Perl code used in onBOOT() function here; calls to XSUBs are
# prototype-checked.
onBOOT;
# Put Perl initialization code assuming that XS is initialized here
see? manageable. slightly chaotic but manageable. π
π₯ DIAGNOSTICS π₯
"Can't find '%s' symbol in %s"β (F) the bootstrap symbol could not be found in the extension module. it's not there, bestie. it's just not there. π"Can't load '%s' for module %s: %s"β (F) the loading or initialisation of the extension module failed. the detailed error follows. read the follow-up, it's telling you everything."Undefined symbols present after loading %s: %s"β (W) as the message says, some symbols stay undefined although the extension module was correctly loaded and initialised. the list of undefined symbols follows. like, everything worked but also there are ghosts. π»
π LIMITATIONS π
to reduce the overhead as much as possible, only one possible location is checked to find the extension DLL (this location is where "make install" would put the DLL). one location. only one. that's the whole search budget. π€
if not found, the search for the DLL is transparently delegated to "DynaLoader", which looks for the DLL along the @INC list.
in particular, this is applicable to the structure of @INC used for testing not-yet-installed extensions. this means that running uninstalled extensions may have much more overhead than running the same extensions after "make install". so like, yeah, dev mode is chunky. that's the tradeoff. π«
π KNOWN BUGS π
the new simpler way to call XSLoader::load() with no arguments at all does not work on Perl 5.8.4 and 5.8.5. those two specific versions. just those. we don't make the rules. π
π BUGS π
please report any bugs or feature requests via the perlbug(1) utility. use the tool, it exists for exactly this. π
π SEE ALSO π
DynaLoader β the big sibling. go say hi.
π AUTHORS π
Ilya Zakharevich originally extracted "XSLoader" from "DynaLoader". a legend, honestly.
CPAN version is currently maintained by SΓ©bastien Aperghis-Tramoni <sebastien@aperghis.net>.
previous maintainer was Michael G Schwern <schwern@pobox.com>.
π COPYRIGHT & LICENSE π
Copyright (C) 1990-2011 by Larry Wall and others.
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. πΈ
and that's the whole page, bestie β perl v5.42.3, dated 2026-11-03, filed under XSLoader(3pm) in the Perl Programmers Reference Guide. mwah π go load some C libraries, you absolute icon π«Άβ¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms