XSLoader(3perl)

๐ŸŒธ XSLoader ๐ŸŒธ

okay bestie, welcome to XSLoader โ€” the Perl Programmers Reference Guide page for it, no less. She's tiny, she's fast, she's literally the module that hauls your C libraries into Perl land. ๐Ÿ’…

XSLoader โ€” Dynamically load C libraries into Perl code


โœจ VERSION โœจ

Version 0.32 ๐Ÿ’– (yes, that's the one, that's the vibe)


โœจ SYNOPSIS โœจ

package YourPackage;
require XSLoader;

XSLoader::load(__PACKAGE__, $VERSION);

look at that. three lines. slay. ๐ŸŽ€


โœจ DESCRIPTION โœจ

so this module defines a standard simplified interface to the dynamic linking mechanisms that lots of platforms have. its whole main-character purpose? implementing cheap automatic dynamic loading of Perl modules. Like, cheap as in low overhead, not cheap as in sketchy. ๐Ÿซถ

if you want the messy, complicated interface, go see DynaLoader. because ngl โ€” many (most) features of DynaLoader are not implemented in XSLoader. For example, the dl_load_flags? not honored by XSLoader. at all. ๐Ÿ™ˆ

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, the glow-up checklist ๐Ÿ“:

  • replace require DynaLoader with use XSLoader
  • remove DynaLoader from @ISA
  • swap bootstrap for 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 above). the way that comma is just... essential. ๐Ÿ˜ญ

of course, if @ISA contained only DynaLoader, there's no need for the @ISA assignment at all. and more than that โ€” 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. two-for-one deletion, iconic. โœจ

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 figure out which package you meant. she just knows. ๐Ÿ”ฎ

Backward compatible boilerplate

want to have your cake and eat it too? tastes great, but it needs a more complicated boilerplate, bestie.

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()'s arguments are needed, because we replaced use XSLoader with require โ€” so the compiler has no idea that a function XSLoader::load() exists. rude of it, honestly. ๐Ÿ’€

this boilerplate uses the low-overhead XSLoader if it's present; if it's used with an antique Perl that has no XSLoader, it falls back to using DynaLoader. graceful. 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).

and heads up: what's described here is equally applicable to the DynaLoader interface. same energy, different module. ๐Ÿ’œ

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. no cap. โš ๏ธ

the call to XSLoader::load() (or bootstrap()) calls the module's bootstrap code. for modules built by xsubpp (which is nearly all modules) this has three side effects:

  • a sanity check is done to make sure the versions of the .pm and the (compiled) .xs parts are compatible. if $VERSION was specified, that's what gets used for the check. if it wasn't 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 .xs file, the code in there gets called.

so consequently โ€” if the code in the .pm file makes calls to these XSUBs, it's convenient to have the XSUBs installed before the Perl code is defined. for example, this makes prototypes for XSUBs visible to that 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, bestie. always has. ๐Ÿ˜Œ

the first situation is way more frequent, so 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 your Perl code is more complicated than the above (e.g. the BOOT: section makes calls to Perl functions which make calls to XSUBs with prototypes), then: get rid of the BOOT: section altogether. ๐Ÿ˜ค bye!

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

literally unhinged but it works. ๐Ÿงš


โœจ DIAGNOSTICS โœจ

  • "Can't find '%s' symbol in %s" โ€” (F) the bootstrap symbol could not be found in the extension module. it's just not in there. gone. ๐Ÿ”
  • "Can't load '%s' for module %s: %s" โ€” (F) the loading or initialisation of the extension module failed. the detailed error follows. ๐Ÿ˜ญ
  • "Undefined symbols present after loading %s: %s" โ€” (W) as the message says, some symbols stay undefined even though the extension module was correctly loaded and initialised. the list of undefined symbols follows. spooky but it's only a warning. ๐Ÿ‘ป

โœจ LIMITATIONS โœจ

to reduce the overhead as much as possible, only one possible location is checked to find the extension DLL (and that location is where make install would put the DLL). if it's not found there, the search for the DLL is transparently delegated to DynaLoader, which looks for the DLL along the @INC list. ๐Ÿ•ต๏ธ

in particular, this applies to the structure of @INC used for testing not-yet-installed extensions. which means โ€” running uninstalled extensions may have much more overhead than running the same extensions after make install. the struggle is real. ๐Ÿ˜ฎ๐Ÿ’จ


โœจ 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 versions said "absolutely not." ๐Ÿšซ


โœจ BUGS โœจ

please report any bugs or feature requests via the perlbug(1) utility. we want to hear it, fr fr. ๐Ÿซถ


โœจ SEE ALSO โœจ

DynaLoader


โœจ AUTHORS โœจ

Ilya Zakharevich originally extracted XSLoader from DynaLoader. king behavior. ๐Ÿ‘‘

CPAN version is currently maintained by Sรฉbastien Aperghis-Tramoni <sebastien@aperghis.net>.

Previous maintainer was Michael G Schwern <schwern@pobox.com>.


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, babes โœจ this was XSLoader for perl v5.44.0, dated 2026-10-17 โ€” go forth and load those C libraries dynamically, you absolute icon ๐Ÿ’…๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms