Install Perl 5.8.8
[msysgit.git] / mingw / html / ext / DynaLoader / DynaLoader.html
blobe6842d73540ff29a1c788ec9d17df74108de80c8
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>DynaLoader - Dynamically load C libraries into Perl code</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;DynaLoader - Dynamically load C libraries into Perl code</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <li><a href="#author">AUTHOR</a></li>
26 </ul>
27 <!-- INDEX END -->
29 <hr />
30 <p>
31 </p>
32 <h1><a name="name">NAME</a></h1>
33 <p>DynaLoader - Dynamically load C libraries into Perl code</p>
34 <p>
35 </p>
36 <hr />
37 <h1><a name="synopsis">SYNOPSIS</a></h1>
38 <pre>
39 package YourPackage;
40 require DynaLoader;
41 @ISA = qw(... DynaLoader ...);
42 bootstrap YourPackage;</pre>
43 <pre>
44 # optional method for 'global' loading
45 sub dl_load_flags { 0x01 }</pre>
46 <p>
47 </p>
48 <hr />
49 <h1><a name="description">DESCRIPTION</a></h1>
50 <p>This document defines a standard generic interface to the dynamic
51 linking mechanisms available on many platforms. Its primary purpose is
52 to implement automatic dynamic loading of Perl modules.</p>
53 <p>This document serves as both a specification for anyone wishing to
54 implement the DynaLoader for a new platform and as a guide for
55 anyone wishing to use the DynaLoader directly in an application.</p>
56 <p>The DynaLoader is designed to be a very simple high-level
57 interface that is sufficiently general to cover the requirements
58 of SunOS, HP-UX, NeXT, Linux, VMS and other platforms.</p>
59 <p>It is also hoped that the interface will cover the needs of OS/2, NT
60 etc and also allow pseudo-dynamic linking (using <code>ld -A</code> at runtime).</p>
61 <p>It must be stressed that the DynaLoader, by itself, is practically
62 useless for accessing non-Perl libraries because it provides almost no
63 Perl-to-C 'glue'. There is, for example, no mechanism for calling a C
64 library function or supplying arguments. A C::DynaLib module
65 is available from CPAN sites which performs that function for some
66 common system types. And since the year 2000, there's also Inline::C,
67 a module that allows you to write Perl subroutines in C. Also available
68 from your local CPAN site.</p>
69 <p>DynaLoader Interface Summary</p>
70 <pre>
71 @dl_library_path
72 @dl_resolve_using
73 @dl_require_symbols
74 $dl_debug
75 @dl_librefs
76 @dl_modules
77 @dl_shared_objects
78 Implemented in:
79 bootstrap($modulename) Perl
80 @filepaths = dl_findfile(@names) Perl
81 $flags = $modulename-&gt;dl_load_flags Perl
82 $symref = dl_find_symbol_anywhere($symbol) Perl</pre>
83 <pre>
84 $libref = dl_load_file($filename, $flags) C
85 $status = dl_unload_file($libref) C
86 $symref = dl_find_symbol($libref, $symbol) C
87 @symbols = dl_undef_symbols() C
88 dl_install_xsub($name, $symref [, $filename]) C
89 $message = dl_error C</pre>
90 <dl>
91 <dt><strong><a name="item__dl_library_path">@dl_library_path</a></strong>
93 <dd>
94 <p>The standard/default list of directories in which <a href="#item_dl_findfile"><code>dl_findfile()</code></a> will
95 search for libraries etc. Directories are searched in order:
96 $dl_library_path[0], [1], ... etc</p>
97 </dd>
98 <dd>
99 <p>@dl_library_path is initialised to hold the list of 'normal' directories
100 (<em>/usr/lib</em>, etc) determined by <strong>Configure</strong> (<code>$Config{'libpth'}</code>). This should
101 ensure portability across a wide range of platforms.</p>
102 </dd>
103 <dd>
104 <p>@dl_library_path should also be initialised with any other directories
105 that can be determined from the environment at runtime (such as
106 LD_LIBRARY_PATH for SunOS).</p>
107 </dd>
108 <dd>
109 <p>After initialisation @dl_library_path can be manipulated by an
110 application using push and unshift before calling dl_findfile().
111 Unshift can be used to add directories to the front of the search order
112 either to save search time or to override libraries with the same name
113 in the 'normal' directories.</p>
114 </dd>
115 <dd>
116 <p>The load function that <a href="#item_dl_load_file"><code>dl_load_file()</code></a> calls may require an absolute
117 pathname. The <a href="#item_dl_findfile"><code>dl_findfile()</code></a> function and @dl_library_path can be
118 used to search for and return the absolute pathname for the
119 library/object that you wish to load.</p>
120 </dd>
121 </li>
122 <dt><strong><a name="item__dl_resolve_using">@dl_resolve_using</a></strong>
124 <dd>
125 <p>A list of additional libraries or other shared objects which can be
126 used to resolve any undefined symbols that might be generated by a
127 later call to load_file().</p>
128 </dd>
129 <dd>
130 <p>This is only required on some platforms which do not handle dependent
131 libraries automatically. For example the Socket Perl extension
132 library (<em>auto/Socket/Socket.so</em>) contains references to many socket
133 functions which need to be resolved when it's loaded. Most platforms
134 will automatically know where to find the 'dependent' library (e.g.,
135 <em>/usr/lib/libsocket.so</em>). A few platforms need to be told the
136 location of the dependent library explicitly. Use @dl_resolve_using
137 for this.</p>
138 </dd>
139 <dd>
140 <p>Example usage:</p>
141 </dd>
142 <dd>
143 <pre>
144 @dl_resolve_using = dl_findfile('-lsocket');</pre>
145 </dd>
146 </li>
147 <dt><strong><a name="item__dl_require_symbols">@dl_require_symbols</a></strong>
149 <dd>
150 <p>A list of one or more symbol names that are in the library/object file
151 to be dynamically loaded. This is only required on some platforms.</p>
152 </dd>
153 </li>
154 <dt><strong><a name="item__dl_librefs">@dl_librefs</a></strong>
156 <dd>
157 <p>An array of the handles returned by successful calls to dl_load_file(),
158 made by bootstrap, in the order in which they were loaded.
159 Can be used with <a href="#item_dl_find_symbol"><code>dl_find_symbol()</code></a> to look for a symbol in any of
160 the loaded files.</p>
161 </dd>
162 </li>
163 <dt><strong><a name="item__dl_modules">@dl_modules</a></strong>
165 <dd>
166 <p>An array of module (package) names that have been bootstrap'ed.</p>
167 </dd>
168 </li>
169 <dt><strong><a name="item__dl_shared_objects">@dl_shared_objects</a></strong>
171 <dd>
172 <p>An array of file names for the shared objects that were loaded.</p>
173 </dd>
174 </li>
175 <dt><strong><a name="item_dl_error"><code>dl_error()</code></a></strong>
177 <dd>
178 <p>Syntax:</p>
179 </dd>
180 <dd>
181 <pre>
182 $message = dl_error();</pre>
183 </dd>
184 <dd>
185 <p>Error message text from the last failed DynaLoader function. Note
186 that, similar to errno in unix, a successful function call does not
187 reset this message.</p>
188 </dd>
189 <dd>
190 <p>Implementations should detect the error as soon as it occurs in any of
191 the other functions and save the corresponding message for later
192 retrieval. This will avoid problems on some platforms (such as SunOS)
193 where the error message is very temporary (e.g., dlerror()).</p>
194 </dd>
195 </li>
196 <dt><strong><a name="item__dl_debug">$dl_debug</a></strong>
198 <dd>
199 <p>Internal debugging messages are enabled when $dl_debug is set true.
200 Currently setting $dl_debug only affects the Perl side of the
201 DynaLoader. These messages should help an application developer to
202 resolve any DynaLoader usage problems.</p>
203 </dd>
204 <dd>
205 <p>$dl_debug is set to <code>$ENV{'PERL_DL_DEBUG'}</code> if defined.</p>
206 </dd>
207 <dd>
208 <p>For the DynaLoader developer/porter there is a similar debugging
209 variable added to the C code (see dlutils.c) and enabled if Perl was
210 built with the <strong>-DDEBUGGING</strong> flag. This can also be set via the
211 PERL_DL_DEBUG environment variable. Set to 1 for minimal information or
212 higher for more.</p>
213 </dd>
214 </li>
215 <dt><strong><a name="item_dl_findfile"><code>dl_findfile()</code></a></strong>
217 <dd>
218 <p>Syntax:</p>
219 </dd>
220 <dd>
221 <pre>
222 @filepaths = dl_findfile(@names)</pre>
223 </dd>
224 <dd>
225 <p>Determine the full paths (including file suffix) of one or more
226 loadable files given their generic names and optionally one or more
227 directories. Searches directories in @dl_library_path by default and
228 returns an empty list if no files were found.</p>
229 </dd>
230 <dd>
231 <p>Names can be specified in a variety of platform independent forms. Any
232 names in the form <strong>-lname</strong> are converted into <em>libname.*</em>, where <em>.*</em> is
233 an appropriate suffix for the platform.</p>
234 </dd>
235 <dd>
236 <p>If a name does not already have a suitable prefix and/or suffix then
237 the corresponding file will be searched for by trying combinations of
238 prefix and suffix appropriate to the platform: ``$name.o'', ``lib$name.*''
239 and ``$name''.</p>
240 </dd>
241 <dd>
242 <p>If any directories are included in @names they are searched before
243 @dl_library_path. Directories may be specified as <strong>-Ldir</strong>. Any other
244 names are treated as filenames to be searched for.</p>
245 </dd>
246 <dd>
247 <p>Using arguments of the form <code>-Ldir</code> and <code>-lname</code> is recommended.</p>
248 </dd>
249 <dd>
250 <p>Example:</p>
251 </dd>
252 <dd>
253 <pre>
254 @dl_resolve_using = dl_findfile(qw(-L/usr/5lib -lposix));</pre>
255 </dd>
256 </li>
257 <dt><strong><a name="item_dl_expandspec"><code>dl_expandspec()</code></a></strong>
259 <dd>
260 <p>Syntax:</p>
261 </dd>
262 <dd>
263 <pre>
264 $filepath = dl_expandspec($spec)</pre>
265 </dd>
266 <dd>
267 <p>Some unusual systems, such as VMS, require special filename handling in
268 order to deal with symbolic names for files (i.e., VMS's Logical Names).</p>
269 </dd>
270 <dd>
271 <p>To support these systems a <a href="#item_dl_expandspec"><code>dl_expandspec()</code></a> function can be implemented
272 either in the <em>dl_*.xs</em> file or code can be added to the autoloadable
273 <a href="#item_dl_expandspec"><code>dl_expandspec()</code></a> function in <em>DynaLoader.pm</em>. See <em>DynaLoader.pm</em> for
274 more information.</p>
275 </dd>
276 </li>
277 <dt><strong><a name="item_dl_load_file"><code>dl_load_file()</code></a></strong>
279 <dd>
280 <p>Syntax:</p>
281 </dd>
282 <dd>
283 <pre>
284 $libref = dl_load_file($filename, $flags)</pre>
285 </dd>
286 <dd>
287 <p>Dynamically load $filename, which must be the path to a shared object
288 or library. An opaque 'library reference' is returned as a handle for
289 the loaded object. Returns undef on error.</p>
290 </dd>
291 <dd>
292 <p>The $flags argument to alters dl_load_file behaviour.
293 Assigned bits:</p>
294 </dd>
295 <dd>
296 <pre>
297 0x01 make symbols available for linking later dl_load_file's.
298 (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL))
299 (ignored under VMS; this is a normal part of image linking)</pre>
300 </dd>
301 <dd>
302 <p>(On systems that provide a handle for the loaded object such as SunOS
303 and HPUX, $libref will be that handle. On other systems $libref will
304 typically be $filename or a pointer to a buffer containing $filename.
305 The application should not examine or alter $libref in any way.)</p>
306 </dd>
307 <dd>
308 <p>This is the function that does the real work. It should use the
309 current values of @dl_require_symbols and @dl_resolve_using if required.</p>
310 </dd>
311 <dd>
312 <pre>
313 SunOS: dlopen($filename)
314 HP-UX: shl_load($filename)
315 Linux: dld_create_reference(@dl_require_symbols); dld_link($filename)
316 NeXT: rld_load($filename, @dl_resolve_using)
317 VMS: lib$find_image_symbol($filename,$dl_require_symbols[0])</pre>
318 </dd>
319 <dd>
320 <p>(The <code>dlopen()</code> function is also used by Solaris and some versions of
321 Linux, and is a common choice when providing a ``wrapper'' on other
322 mechanisms as is done in the OS/2 port.)</p>
323 </dd>
324 </li>
325 <dt><strong><a name="item_dl_unload_file"><code>dl_unload_file()</code></a></strong>
327 <dd>
328 <p>Syntax:</p>
329 </dd>
330 <dd>
331 <pre>
332 $status = dl_unload_file($libref)</pre>
333 </dd>
334 <dd>
335 <p>Dynamically unload $libref, which must be an opaque 'library reference' as
336 returned from dl_load_file. Returns one on success and zero on failure.</p>
337 </dd>
338 <dd>
339 <p>This function is optional and may not necessarily be provided on all platforms.
340 If it is defined, it is called automatically when the interpreter exits for
341 every shared object or library loaded by DynaLoader::bootstrap. All such
342 library references are stored in @dl_librefs by DynaLoader::Bootstrap as it
343 loads the libraries. The files are unloaded in last-in, first-out order.</p>
344 </dd>
345 <dd>
346 <p>This unloading is usually necessary when embedding a shared-object perl (e.g.
347 one configured with -Duseshrplib) within a larger application, and the perl
348 interpreter is created and destroyed several times within the lifetime of the
349 application. In this case it is possible that the system dynamic linker will
350 unload and then subsequently reload the shared libperl without relocating any
351 references to it from any files DynaLoaded by the previous incarnation of the
352 interpreter. As a result, any shared objects opened by DynaLoader may point to
353 a now invalid 'ghost' of the libperl shared object, causing apparently random
354 memory corruption and crashes. This behaviour is most commonly seen when using
355 Apache and mod_perl built with the APXS mechanism.</p>
356 </dd>
357 <dd>
358 <pre>
359 SunOS: dlclose($libref)
360 HP-UX: ???
361 Linux: ???
362 NeXT: ???
363 VMS: ???</pre>
364 </dd>
365 <dd>
366 <p>(The <code>dlclose()</code> function is also used by Solaris and some versions of
367 Linux, and is a common choice when providing a ``wrapper'' on other
368 mechanisms as is done in the OS/2 port.)</p>
369 </dd>
370 </li>
371 <dt><strong><a name="item_dl_load_flags"><code>dl_load_flags()</code></a></strong>
373 <dd>
374 <p>Syntax:</p>
375 </dd>
376 <dd>
377 <pre>
378 $flags = dl_load_flags $modulename;</pre>
379 </dd>
380 <dd>
381 <p>Designed to be a method call, and to be overridden by a derived class
382 (i.e. a class which has DynaLoader in its @ISA). The definition in
383 DynaLoader itself returns 0, which produces standard behavior from
384 dl_load_file().</p>
385 </dd>
386 </li>
387 <dt><strong><a name="item_dl_find_symbol"><code>dl_find_symbol()</code></a></strong>
389 <dd>
390 <p>Syntax:</p>
391 </dd>
392 <dd>
393 <pre>
394 $symref = dl_find_symbol($libref, $symbol)</pre>
395 </dd>
396 <dd>
397 <p>Return the address of the symbol $symbol or <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a> if not found. If the
398 target system has separate functions to search for symbols of different
399 types then <a href="#item_dl_find_symbol"><code>dl_find_symbol()</code></a> should search for function symbols first and
400 then other types.</p>
401 </dd>
402 <dd>
403 <p>The exact manner in which the address is returned in $symref is not
404 currently defined. The only initial requirement is that $symref can
405 be passed to, and understood by, dl_install_xsub().</p>
406 </dd>
407 <dd>
408 <pre>
409 SunOS: dlsym($libref, $symbol)
410 HP-UX: shl_findsym($libref, $symbol)
411 Linux: dld_get_func($symbol) and/or dld_get_symbol($symbol)
412 NeXT: rld_lookup(&quot;_$symbol&quot;)
413 VMS: lib$find_image_symbol($libref,$symbol)</pre>
414 </dd>
415 </li>
416 <dt><strong><a name="item_dl_find_symbol_anywhere"><code>dl_find_symbol_anywhere()</code></a></strong>
418 <dd>
419 <p>Syntax:</p>
420 </dd>
421 <dd>
422 <pre>
423 $symref = dl_find_symbol_anywhere($symbol)</pre>
424 </dd>
425 <dd>
426 <p>Applies <a href="#item_dl_find_symbol"><code>dl_find_symbol()</code></a> to the members of @dl_librefs and returns
427 the first match found.</p>
428 </dd>
429 </li>
430 <dt><strong><a name="item_dl_undef_symbols"><code>dl_undef_symbols()</code></a></strong>
432 <dd>
433 <p>Example</p>
434 </dd>
435 <dd>
436 <pre>
437 @symbols = dl_undef_symbols()</pre>
438 </dd>
439 <dd>
440 <p>Return a list of symbol names which remain undefined after load_file().
441 Returns <code>()</code> if not known. Don't worry if your platform does not provide
442 a mechanism for this. Most do not need it and hence do not provide it,
443 they just return an empty list.</p>
444 </dd>
445 </li>
446 <dt><strong><a name="item_dl_install_xsub"><code>dl_install_xsub()</code></a></strong>
448 <dd>
449 <p>Syntax:</p>
450 </dd>
451 <dd>
452 <pre>
453 dl_install_xsub($perl_name, $symref [, $filename])</pre>
454 </dd>
455 <dd>
456 <p>Create a new Perl external subroutine named $perl_name using $symref as
457 a pointer to the function which implements the routine. This is simply
458 a direct call to newXSUB(). Returns a reference to the installed
459 function.</p>
460 </dd>
461 <dd>
462 <p>The $filename parameter is used by Perl to identify the source file for
463 the function if required by die(), <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_caller"><code>caller()</code></a> or the debugger. If
464 $filename is not defined then ``DynaLoader'' will be used.</p>
465 </dd>
466 </li>
467 <dt><strong><a name="item_bootstrap"><code>bootstrap()</code></a></strong>
469 <dd>
470 <p>Syntax:</p>
471 </dd>
472 <dd>
473 <p><a href="#item_bootstrap"><code>bootstrap($module)</code></a></p>
474 </dd>
475 <dd>
476 <p>This is the normal entry point for automatic dynamic loading in Perl.</p>
477 </dd>
478 <dd>
479 <p>It performs the following actions:</p>
480 </dd>
481 <ul>
482 <li>
483 <p>locates an auto/$module directory by searching @INC</p>
484 </li>
485 <li>
486 <p>uses <a href="#item_dl_findfile"><code>dl_findfile()</code></a> to determine the filename to load</p>
487 </li>
488 <li>
489 <p>sets @dl_require_symbols to <code>(&quot;boot_$module&quot;)</code></p>
490 </li>
491 <li>
492 <p>executes an <em>auto/$module/$module.bs</em> file if it exists
493 (typically used to add to @dl_resolve_using any files which
494 are required to load the module on the current platform)</p>
495 </li>
496 <li>
497 <p>calls <a href="#item_dl_load_flags"><code>dl_load_flags()</code></a> to determine how to load the file.</p>
498 </li>
499 <li>
500 <p>calls <a href="#item_dl_load_file"><code>dl_load_file()</code></a> to load the file</p>
501 </li>
502 <li>
503 <p>calls <a href="#item_dl_undef_symbols"><code>dl_undef_symbols()</code></a> and warns if any symbols are undefined</p>
504 </li>
505 <li>
506 <p>calls <a href="#item_dl_find_symbol"><code>dl_find_symbol()</code></a> for ``boot_$module''</p>
507 </li>
508 <li>
509 <p>calls <a href="#item_dl_install_xsub"><code>dl_install_xsub()</code></a> to install it as ``${module}::bootstrap''</p>
510 </li>
511 <li>
512 <p>calls &amp;{``${module}::bootstrap''} to bootstrap the module (actually
513 it uses the function reference returned by dl_install_xsub for speed)</p>
514 </li>
515 </ul>
516 </dl>
518 </p>
519 <hr />
520 <h1><a name="author">AUTHOR</a></h1>
521 <p>Tim Bunce, 11 August 1994.</p>
522 <p>This interface is based on the work and comments of (in no particular
523 order): Larry Wall, Robert Sanders, Dean Roehrich, Jeff Okamoto, Anno
524 Siegel, Thomas Neumann, Paul Marquess, Charles Bailey, myself and others.</p>
525 <p>Larry Wall designed the elegant inherited bootstrap mechanism and
526 implemented the first Perl 5 dynamic loader using it.</p>
527 <p>Solaris global loading added by Nick Ing-Simmons with design/coding
528 assistance from Tim Bunce, January 1996.</p>
529 <table border="0" width="100%" cellspacing="0" cellpadding="3">
530 <tr><td class="block" style="background-color: #cccccc" valign="middle">
531 <big><strong><span class="block">&nbsp;DynaLoader - Dynamically load C libraries into Perl code</span></strong></big>
532 </td></tr>
533 </table>
535 </body>
537 </html>