1XSLoader(3pm) Perl Programmers Reference Guide XSLoader(3pm)
2
3
4
6 XSLoader - Dynamically load C libraries into Perl code
7
9 Version 0.32
10
12 package YourPackage;
13 require XSLoader;
14
15 XSLoader::load(__PACKAGE__, $VERSION);
16
18 This module defines a standard simplified interface to the dynamic
19 linking mechanisms available on many platforms. Its primary purpose is
20 to implement cheap automatic dynamic loading of Perl modules.
21
22 For a more complicated interface, see DynaLoader. Many (most) features
23 of "DynaLoader" are not implemented in "XSLoader", like for example the
24 "dl_load_flags", not honored by "XSLoader".
25
26 Migration from "DynaLoader"
27 A typical module using DynaLoader starts like this:
28
29 package YourPackage;
30 require DynaLoader;
31
32 our @ISA = qw( OnePackage OtherPackage DynaLoader );
33 our $VERSION = '0.01';
34 __PACKAGE__->bootstrap($VERSION);
35
36 Change this to
37
38 package YourPackage;
39 use XSLoader;
40
41 our @ISA = qw( OnePackage OtherPackage );
42 our $VERSION = '0.01';
43 XSLoader::load(__PACKAGE__, $VERSION);
44
45 In other words: replace "require DynaLoader" by "use XSLoader", remove
46 "DynaLoader" from @ISA, change "bootstrap" by "XSLoader::load". Do not
47 forget to quote the name of your package on the "XSLoader::load" line,
48 and add comma (",") before the arguments ($VERSION above).
49
50 Of course, if @ISA contained only "DynaLoader", there is no need to
51 have the @ISA assignment at all; moreover, if instead of "our" one uses
52 the more backward-compatible
53
54 use vars qw($VERSION @ISA);
55
56 one can remove this reference to @ISA together with the @ISA
57 assignment.
58
59 If no $VERSION was specified on the "bootstrap" line, the last line
60 becomes
61
62 XSLoader::load(__PACKAGE__);
63
64 in which case it can be further simplified to
65
66 XSLoader::load();
67
68 as "load" will use "caller" to determine the package.
69
70 Backward compatible boilerplate
71 If you want to have your cake and eat it too, you need a more
72 complicated boilerplate.
73
74 package YourPackage;
75
76 our @ISA = qw( OnePackage OtherPackage );
77 our $VERSION = '0.01';
78 eval {
79 require XSLoader;
80 XSLoader::load(__PACKAGE__, $VERSION);
81 1;
82 } or do {
83 require DynaLoader;
84 push @ISA, 'DynaLoader';
85 __PACKAGE__->bootstrap($VERSION);
86 };
87
88 The parentheses about XSLoader::load() arguments are needed since we
89 replaced "use XSLoader" by "require", so the compiler does not know
90 that a function XSLoader::load() is present.
91
92 This boilerplate uses the low-overhead "XSLoader" if present; if used
93 with an antique Perl which has no "XSLoader", it falls back to using
94 "DynaLoader".
95
97 Skip this section if the XSUB functions are supposed to be called from
98 other modules only; read it only if you call your XSUBs from the code
99 in your module, or have a "BOOT:" section in your XS file (see "The
100 BOOT: Keyword" in perlxs). What is described here is equally
101 applicable to the DynaLoader interface.
102
103 A sufficiently complicated module using XS would have both Perl code
104 (defined in YourPackage.pm) and XS code (defined in YourPackage.xs).
105 If this Perl code makes calls into this XS code, and/or this XS code
106 makes calls to the Perl code, one should be careful with the order of
107 initialization.
108
109 The call to XSLoader::load() (or bootstrap()) calls the module's
110 bootstrap code. For modules build by xsubpp (nearly all modules) this
111 has three side effects:
112
113 • A sanity check is done to ensure that the versions of the .pm and
114 the (compiled) .xs parts are compatible. If $VERSION was specified,
115 this is used for the check. If not specified, it defaults to
116 "$XS_VERSION // $VERSION" (in the module's namespace)
117
118 • the XSUBs are made accessible from Perl
119
120 • if a "BOOT:" section was present in the .xs file, the code there is
121 called.
122
123 Consequently, if the code in the .pm file makes calls to these XSUBs,
124 it is convenient to have XSUBs installed before the Perl code is
125 defined; for example, this makes prototypes for XSUBs visible to this
126 Perl code. Alternatively, if the "BOOT:" section makes calls to Perl
127 functions (or uses Perl variables) defined in the .pm file, they must
128 be defined prior to the call to XSLoader::load() (or bootstrap()).
129
130 The first situation being much more frequent, it makes sense to rewrite
131 the boilerplate as
132
133 package YourPackage;
134 use XSLoader;
135 our ($VERSION, @ISA);
136
137 BEGIN {
138 @ISA = qw( OnePackage OtherPackage );
139 $VERSION = '0.01';
140
141 # Put Perl code used in the BOOT: section here
142
143 XSLoader::load(__PACKAGE__, $VERSION);
144 }
145
146 # Put Perl code making calls into XSUBs here
147
148 The most hairy case
149 If the interdependence of your "BOOT:" section and Perl code is more
150 complicated than this (e.g., the "BOOT:" section makes calls to Perl
151 functions which make calls to XSUBs with prototypes), get rid of the
152 "BOOT:" section altogether. Replace it with a function onBOOT(), and
153 call it like this:
154
155 package YourPackage;
156 use XSLoader;
157 our ($VERSION, @ISA);
158
159 BEGIN {
160 @ISA = qw( OnePackage OtherPackage );
161 $VERSION = '0.01';
162 XSLoader::load(__PACKAGE__, $VERSION);
163 }
164
165 # Put Perl code used in onBOOT() function here; calls to XSUBs are
166 # prototype-checked.
167
168 onBOOT;
169
170 # Put Perl initialization code assuming that XS is initialized here
171
173 "Can't find '%s' symbol in %s"
174 (F) The bootstrap symbol could not be found in the extension
175 module.
176
177 "Can't load '%s' for module %s: %s"
178 (F) The loading or initialisation of the extension module failed.
179 The detailed error follows.
180
181 "Undefined symbols present after loading %s: %s"
182 (W) As the message says, some symbols stay undefined although the
183 extension module was correctly loaded and initialised. The list of
184 undefined symbols follows.
185
187 To reduce the overhead as much as possible, only one possible location
188 is checked to find the extension DLL (this location is where "make
189 install" would put the DLL). If not found, the search for the DLL is
190 transparently delegated to "DynaLoader", which looks for the DLL along
191 the @INC list.
192
193 In particular, this is applicable to the structure of @INC used for
194 testing not-yet-installed extensions. This means that running
195 uninstalled extensions may have much more overhead than running the
196 same extensions after "make install".
197
199 The new simpler way to call XSLoader::load() with no arguments at all
200 does not work on Perl 5.8.4 and 5.8.5.
201
203 Please report any bugs or feature requests via the perlbug(1) utility.
204
206 DynaLoader
207
209 Ilya Zakharevich originally extracted "XSLoader" from "DynaLoader".
210
211 CPAN version is currently maintained by Sébastien Aperghis-Tramoni
212 <sebastien@aperghis.net>.
213
214 Previous maintainer was Michael G Schwern <schwern@pobox.com>.
215
217 Copyright (C) 1990-2011 by Larry Wall and others.
218
219 This program is free software; you can redistribute it and/or modify it
220 under the same terms as Perl itself.
221
222
223
224perl v5.38.2 2023-11-30 XSLoader(3pm)