|
B |
Detect::Module - allows to autodetect Perl modules
use Detect::Module qw(:standard);
my $which = Use 'DB_File', 'SDBM_File';
# $which now contains the name of the loaded module
# This is specially useful because the modules have
# a compatible tie() syntax
my $constructor = NewRef 'IO::Socket::COOL', 'IO::Socket::INET';
my $o = $constructor->(PeerAddr => 'localhost:25');
# $o now contains an object of one of the both modules
my $esc = Load 'URI::Escape';
$esc->uri_escape (' ') eq '%20';
${$esc->('SCALAR', 'x')} = 3;
This module is used for autodetection of Perl modules. The functions
in this module accept a list of module names, from which the first
one that can be loaded using require() is used.
You may include most functions using the export tag
':standard' like shown in the synopsis. Otherwise you call them like
Detect::Module::Use(). Only Debug() and Reset() cannot be exported.
require() except they are executed at run time and all
modules in @ModuleList are tried until an existing module is found.
If no such module exists, these subs die(). The modules in the list
are tried from left to right. Module names have to contain :: as path
separator. Of course the @INC path is used to find the modules
(the built-in require() is used to load the modules).
There is no difference between Use() and Require(). To achieve
compile-time execution, enclose the Use() call in a BEGIN block.
The return value is the name of the module that has been loaded.
Once loading a module has failed, it is never tried again. So using these subs multiple times does not result in performance impacts (Perl checks if a module is already loaded). But you should use this to access multiple functions of this package:
# Load the module
my $mod = Use (...);
# I need the object:
my $obj = Load $mod;
# This does not have performance impacts because $mod is already
# loaded and Perl checks %INC!
You can circumvent this behaviour by calling Detect::Module::Reset which clears the %FAIL-Cache.
There is a trick to catch cases where a module is _not_ found:
# Try loading
unless (my $mod = Use (..., 0))
{
print "Module not found\n";
print "Using workaround instead...\n";
}
This relies upon the fact that a require 0 always succeeds. But this works
only with Use(), Require() and Load()!
Use() except that a reference to the
constructor (or better: to a sub that calls the module's constructor)
is returned. The returned sub reference calls new Module::Name @_,
where Module::Name is the name of the module loaded.
m() of the module with @args.
CHANGED since Detect::Module version 1.2: Load() now returns undef if
no module was found and '0' was a possible name.
require() attempts. This may
be useful when using mod_perl in Apache.
Written by Rudolf Polzer, Germany (rpolzer@durchnull.de)
Load() uses symbolic references (therefore I use no strict 'subs').
Doing the same with eval() leads to more obfuscated code.
Load() return references instead of tied variables.
This makes accessing package variables harder, but not impossible.
AUTOLOAD() using the object returned by Load().
Use $o->(CODE => 'AUTOLOAD')->(@args) instead.
Report bugs to me (rpolzer@durchnull.de)
(c) 2001 Rudolf Polzer. This is free software, copying and modifications are allowed as long as this copyright notice remains. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
perlfunc(1) for use(), require() and do()
|
B |