1# Generated from XSLoader.pm.PL (resolved %Config::Config value)
2# This file is unique for every OS
3
4package XSLoader;
5
6$VERSION = "0.24";
7
8#use strict;
9
10package DynaLoader;
11
12# No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
13# NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
14boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
15                                !defined(&dl_error);
16package XSLoader;
17
18sub load {
19    package DynaLoader;
20
21    my ($caller, $modlibname) = caller();
22    my $module = $caller;
23
24    if (@_) {
25        $module = $_[0];
26    } else {
27        $_[0] = $module;
28    }
29
30    # work with static linking too
31    my $boots = "$module\::bootstrap";
32    goto &$boots if defined &$boots;
33
34    goto \&XSLoader::bootstrap_inherit unless $module and defined &dl_load_file;
35
36    my @modparts = split(/::/,$module);
37    my $modfname = $modparts[-1];
38
39    my $modpname = join('/',@modparts);
40    my $c = () = split(/::/,$caller,-1);
41    $modlibname =~ s,[\\/][^\\/]+$,, while $c--;    # Q&D basename
42    # Does this look like a relative path?
43    if ($modlibname !~ m{^/}) {
44        # Someone may have a #line directive that changes the file name, or
45        # may be calling XSLoader::load from inside a string eval.  We cer-
46        # tainly do not want to go loading some code that is not in @INC,
47        # as it could be untrusted.
48        #
49        # We could just fall back to DynaLoader here, but then the rest of
50        # this function would go untested in the perl core, since all @INC
51        # paths are relative during testing.  That would be a time bomb
52        # waiting to happen, since bugs could be introduced into the code.
53        #
54        # So look through @INC to see if $modlibname is in it.  A rela-
55        # tive $modlibname is not a common occurrence, so this block is
56        # not hot code.
57        FOUND: {
58            for (@INC) {
59                if ($_ eq $modlibname) {
60                    last FOUND;
61                }
62            }
63            # Not found.  Fall back to DynaLoader.
64            goto \&XSLoader::bootstrap_inherit;
65        }
66    }
67    my $file = "$modlibname/auto/$modpname/$modfname.bundle";
68
69#   print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
70
71    my $bs = $file;
72    $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
73
74    if (-s $bs) { # only read file if it's not empty
75#       print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
76        eval { do $bs; };
77        warn "$bs: $@\n" if $@;
78	goto \&XSLoader::bootstrap_inherit;
79    }
80
81    goto \&XSLoader::bootstrap_inherit if not -f $file;
82
83    my $bootname = "boot_$module";
84    $bootname =~ s/\W/_/g;
85    @DynaLoader::dl_require_symbols = ($bootname);
86
87    my $boot_symbol_ref;
88
89    if ($boot_symbol_ref = dl_find_symbol( 0, $bootname )) {
90        goto boot; #extension library has already been loaded, e.g. darwin
91    }
92    # Many dynamic extension loading problems will appear to come from
93    # this section of code: XYZ failed at line 123 of DynaLoader.pm.
94    # Often these errors are actually occurring in the initialisation
95    # C code of the extension XS file. Perl reports the error as being
96    # in this perl code simply because this was the last perl code
97    # it executed.
98
99    my $libref = dl_load_file($file, 0) or do {
100        require Carp;
101        Carp::croak("Can't load '$file' for module $module: " . dl_error());
102    };
103    push(@DynaLoader::dl_librefs,$libref);  # record loaded object
104
105    $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
106        require Carp;
107        Carp::croak("Can't find '$bootname' symbol in $file\n");
108    };
109
110    push(@DynaLoader::dl_modules, $module); # record loaded module
111
112  boot:
113    my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file);
114
115    # See comment block above
116    push(@DynaLoader::dl_shared_objects, $file); # record files loaded
117    return &$xs(@_);
118}
119
120sub bootstrap_inherit {
121    require DynaLoader;
122    goto \&DynaLoader::bootstrap_inherit;
123}
124
1251;
126
127
128__END__
129
130=head1 NAME
131
132XSLoader - Dynamically load C libraries into Perl code
133
134=head1 VERSION
135
136Version 0.24
137
138=head1 SYNOPSIS
139
140    package YourPackage;
141    require XSLoader;
142
143    XSLoader::load();
144
145=head1 DESCRIPTION
146
147This module defines a standard I<simplified> interface to the dynamic
148linking mechanisms available on many platforms.  Its primary purpose is
149to implement cheap automatic dynamic loading of Perl modules.
150
151For a more complicated interface, see L<DynaLoader>.  Many (most)
152features of C<DynaLoader> are not implemented in C<XSLoader>, like for
153example the C<dl_load_flags>, not honored by C<XSLoader>.
154
155=head2 Migration from C<DynaLoader>
156
157A typical module using L<DynaLoader|DynaLoader> starts like this:
158
159    package YourPackage;
160    require DynaLoader;
161
162    our @ISA = qw( OnePackage OtherPackage DynaLoader );
163    our $VERSION = '0.01';
164    bootstrap YourPackage $VERSION;
165
166Change this to
167
168    package YourPackage;
169    use XSLoader;
170
171    our @ISA = qw( OnePackage OtherPackage );
172    our $VERSION = '0.01';
173    XSLoader::load 'YourPackage', $VERSION;
174
175In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
176C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>.  Do not
177forget to quote the name of your package on the C<XSLoader::load> line,
178and add comma (C<,>) before the arguments (C<$VERSION> above).
179
180Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
181the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
182more backward-compatible
183
184    use vars qw($VERSION @ISA);
185
186one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
187
188If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
189
190    XSLoader::load 'YourPackage';
191
192If the call to C<load> is from C<YourPackage>, then that can be further
193simplified to
194
195    XSLoader::load();
196
197as C<load> will use C<caller> to determine the package.
198
199=head2 Backward compatible boilerplate
200
201If you want to have your cake and eat it too, you need a more complicated
202boilerplate.
203
204    package YourPackage;
205    use vars qw($VERSION @ISA);
206
207    @ISA = qw( OnePackage OtherPackage );
208    $VERSION = '0.01';
209    eval {
210       require XSLoader;
211       XSLoader::load('YourPackage', $VERSION);
212       1;
213    } or do {
214       require DynaLoader;
215       push @ISA, 'DynaLoader';
216       bootstrap YourPackage $VERSION;
217    };
218
219The parentheses about C<XSLoader::load()> arguments are needed since we replaced
220C<use XSLoader> by C<require>, so the compiler does not know that a function
221C<XSLoader::load()> is present.
222
223This boilerplate uses the low-overhead C<XSLoader> if present; if used with
224an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
225
226=head1 Order of initialization: early load()
227
228I<Skip this section if the XSUB functions are supposed to be called from other
229modules only; read it only if you call your XSUBs from the code in your module,
230or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
231What is described here is equally applicable to the L<DynaLoader|DynaLoader>
232interface.>
233
234A sufficiently complicated module using XS would have both Perl code (defined
235in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>).  If this
236Perl code makes calls into this XS code, and/or this XS code makes calls to
237the Perl code, one should be careful with the order of initialization.
238
239The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's
240bootstrap code. For modules build by F<xsubpp> (nearly all modules) this
241has three side effects:
242
243=over
244
245=item *
246
247A sanity check is done to ensure that the versions of the F<.pm> and the
248(compiled) F<.xs> parts are compatible. If C<$VERSION> was specified, this
249is used for the check. If not specified, it defaults to
250C<$XS_VERSION // $VERSION> (in the module's namespace)
251
252=item *
253
254the XSUBs are made accessible from Perl
255
256=item *
257
258if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
259
260=back
261
262Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
263convenient to have XSUBs installed before the Perl code is defined; for
264example, this makes prototypes for XSUBs visible to this Perl code.
265Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
266uses Perl variables) defined in the F<.pm> file, they must be defined prior to
267the call to C<XSLoader::load()> (or C<bootstrap()>).
268
269The first situation being much more frequent, it makes sense to rewrite the
270boilerplate as
271
272    package YourPackage;
273    use XSLoader;
274    use vars qw($VERSION @ISA);
275
276    BEGIN {
277       @ISA = qw( OnePackage OtherPackage );
278       $VERSION = '0.01';
279
280       # Put Perl code used in the BOOT: section here
281
282       XSLoader::load 'YourPackage', $VERSION;
283    }
284
285    # Put Perl code making calls into XSUBs here
286
287=head2 The most hairy case
288
289If the interdependence of your C<BOOT:> section and Perl code is
290more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
291functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
292section altogether.  Replace it with a function C<onBOOT()>, and call it like
293this:
294
295    package YourPackage;
296    use XSLoader;
297    use vars qw($VERSION @ISA);
298
299    BEGIN {
300       @ISA = qw( OnePackage OtherPackage );
301       $VERSION = '0.01';
302       XSLoader::load 'YourPackage', $VERSION;
303    }
304
305    # Put Perl code used in onBOOT() function here; calls to XSUBs are
306    # prototype-checked.
307
308    onBOOT;
309
310    # Put Perl initialization code assuming that XS is initialized here
311
312
313=head1 DIAGNOSTICS
314
315=over
316
317=item C<Can't find '%s' symbol in %s>
318
319B<(F)> The bootstrap symbol could not be found in the extension module.
320
321=item C<Can't load '%s' for module %s: %s>
322
323B<(F)> The loading or initialisation of the extension module failed.
324The detailed error follows.
325
326=item C<Undefined symbols present after loading %s: %s>
327
328B<(W)> As the message says, some symbols stay undefined although the
329extension module was correctly loaded and initialised. The list of undefined
330symbols follows.
331
332=back
333
334=head1 LIMITATIONS
335
336To reduce the overhead as much as possible, only one possible location
337is checked to find the extension DLL (this location is where C<make install>
338would put the DLL).  If not found, the search for the DLL is transparently
339delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
340
341In particular, this is applicable to the structure of C<@INC> used for testing
342not-yet-installed extensions.  This means that running uninstalled extensions
343may have much more overhead than running the same extensions after
344C<make install>.
345
346
347=head1 KNOWN BUGS
348
349The new simpler way to call C<XSLoader::load()> with no arguments at all
350does not work on Perl 5.8.4 and 5.8.5.
351
352
353=head1 BUGS
354
355Please report any bugs or feature requests via the perlbug(1) utility.
356
357
358=head1 SEE ALSO
359
360L<DynaLoader>
361
362
363=head1 AUTHORS
364
365Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
366
367CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
368E<lt>sebastien@aperghis.netE<gt>.
369
370Previous maintainer was Michael G Schwern <schwern@pobox.com>.
371
372
373=head1 COPYRIGHT & LICENSE
374
375Copyright (C) 1990-2011 by Larry Wall and others.
376
377This program is free software; you can redistribute it and/or modify
378it under the same terms as Perl itself.
379
380=cut
381