dlopen
my $handle = dlopen($filename, $flags);
This opens a dynamic library in the context of the dynamic loader. $filename is the full or relative
path to a dynamic library (usually a ".so" on Linux and some other UNIXen, a ".dll" on Windows and a
".dylib" on OS X). $flags are flags that can be used to alter the behavior of the library and the
symbols it contains. The return value is an opaque pointer or $handle which can be used to look up
symbols with "dlsym". The handle should be closed with "dlclose" when you are done with it.
By convention if you pass in "undef" for the filename, the currently loaded executable will be used
instead of a separate dynamic library. This is the easiest and most portable way to find the address of
symbols in the standard C library. This convention is baked into most UNIXen, but this capability is
emulated in Windows which doesn't come with the capability out of the box.
If there is an error in opening the library then "undef" will be returned and the diagnostic for the
failure can be retrieved with "dlerror" as described below.
Not all flags are supported on all platforms. You can test if a flag is available using can:
if(FFI::Platypus::DL->can('RTLD_LAZY'))
{
...
}
Typically where flags are not mutually exclusive, they can be or'd together:
my $handle = dlopen("libfoo.so", RTLD_LAZY | RTLD_GLOBAL);
Check your operating system documentation for detailed descriptions of these flags.
RTLD_PLATYPUS_DEFAULT
This is the FFI::Platypus default for "dlopen" (NOTE: NOT the libdl default). This is the only flag
supported on Windows. For historical reasons, this is usually "RTLD_LAZY" on Unix and 0 on Windows.
RTLD_LAZY
Perform lazy binding.
RTLD_NOW
Resolve all symbols before returning from "dlopen". Error if all symbols cannot resolve.
RTLD_GLOBAL
Symbols are shared.
RTLD_LOCAL
Symbols are NOT shared.
RTLD_NODELETE
glibc 2.2 extension.
RTLD_NOLOAD
glibc 2.2 extension.
RTLD_DEEPBIND
glibc 2.3.4 extension.
dlsym
my $opaque = dlsym($handle, $symbol);
This looks up the given $symbol in the library pointed to by $handle. If the symbol is found, the
address for that symbol is returned as an opaque pointer. This pointer can be passed into the
FFI::Platypus "function" and "attach" methods instead of a function name.
If the symbol cannot be found then "undef" will be returned and the diagnostic for the failure can be
retrieved with "dlerror" as described below.
dlclose
my $status = dlclose($handle);
On success, "dlclose" returns 0; on error, it returns a nonzero value, and the diagnostic for the failure
can be retrieved with "dlerror" as described below.
dlerror
my $error_string = dlerror;
Returns the human readable diagnostic for the reason for the failure for the most recent "dl" prefixed
function call.