PTools::Loader - Demand load Perl modules at run-time w/error checking.
This document describes version 0.11, released October, 2004.
By default PTools::Loader will not abort on errors. Add string generror in use statement to cause PTools::Loader to abort when errors are detected.
use PTools::Loader qw(generror); ## Loader will abort on errors $loader = new PTools::Loader; $loader = "PTools::Loader"; $loader->req("Date::Format"); # require module $loader->use("CGI"); # use module $loader->use("CGI", "qw(:standard)"); # use module w/params $loader->eval("filename"); # eval contents of "filename" $loader->ver("5.004"); # check Perl level @result = $loader->eval("filename"); # eval contents of "filename" $codeRef= $loader->codeRef( @result ); # extract code ref from 'eval' $loader->inc("req", "LWP"); # require module $loader->inc("use", "Date::Format"); # use module $loader->inc("use", "CGI ':standard'"); # use module w/params
By default PTools::Loader will not abort on errors, and the abort method must be called. This will simply return when no errors occurred.
or use PTools::Loader; ## Loader won't abort on errors $loader = new PTools::Loader; $loader = "PTools::Loader"; $error = $loader->req("Forms::MainMenu"); # these first three exmamples $error = $loader->use("CGI","2.56"); # will attempt to load the $error = $loader->eval("filename"); # file and return any error @result = $loader->eval("filename"); # eval contents of "filename" $codeRef= $loader->codeRef( @result ); # extract code ref from 'eval' (@err) = $loader->use("Roman"); # collect all info on error @err and $self->cleanup; # for example, # delay abort to run cleanup $loader->abort( @err ); # cause abort IFF any error ($perlError, $loadMode, $moduleName, # collect all info on error $callingPackage, $callingFileName, $callingFileLineNumber) = $loader->use("Mail::Mailer"); ($perlError, $loadMode, $moduleName, $callingPackage, $callingFileName, $callingFileLineNumber, $codeRef) = $loader->eval("Mail::Mailer");
Include a Perl module via "use", "require" or "eval" and detect errs.
None. All functions in this module are implemented as class methods.
Include the specified Perl Module via the use function. Any additional arguments are appended directly to the use function.
Examples:
$loader->use("CGI"); $loader->use("CGI", "qw(:standard)"); $error = $loader->use("CGI","2.56"); (@err) = $loader->use("Roman"); @err and $self->cleanup; $loader->abort( @err ); # only aborts if error occurred
Include the specified Perl Module via the require function.
$loader->inc("req", "LWP"); $error = $loader->req("Forms::MainMenu");
Include the specified Perl File via the eval function.
$loader->eval( $filename ); $error = $loader->eval( $filename );
As this module only contains class methods, the codeRef method is available to extract the code reference from the results of any prior call to the eval method.
use $loader; @results = $loader->eval( $fileName ); $loader->abort( @results ); $codeRef = $loader->codeRef( @results );
The above complete example shows one way to ensure the eval call was successful. The call to abort will only terminate the script when errors were detected during the eval.
If the fileName contains a fully defined package, it is not necessary to obtain the codeRef as the resulting code will be available using its package name space as soon as the eval method completes successfully.
Also see notes on the generror option to the use statement, above, and discussion of the abort method, below.
Include the specified Perl Module based on the Mode.
The Mode parameter must be one of use or req.
The Module parameter is the name of the Perl module, as above.
The Args parameter are optional parms passed to the Perl module, as above.
$loader->inc("req", "LWP"); # require module $loader->inc("use", "Date::Format"); # use module $loader->inc("use", "CGI ':standard'"); # use module w/params
Ensure the required version of the Perl intepreter is currently running.
Example:
$loader->ver("5.004");
The abort method is available to defer aborting the script when errors are detected. This is useful when a "cleanup" step is necessary, for example.
(@err) = $loader->use("Roman"); # collect all info on error @err and $self->cleanup; # delay abort to run cleanup $loader->abort( @err ); # cause abort IFF any error
The parameter list passed to the abort method is designed to be the same as returned from any of the use, req, eval or inc methods.
During the abort, this method emits HTML when run via a Web CGI script. Otherwise it emits plain text.
No classes currently inherit from this class.
Chris Cobb [no dot spam at ccobb dot net]
Copyright (c) 2002-2007 by Chris Cobb. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install PTools, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PTools
CPAN shell
perl -MCPAN -e shell install PTools
For more information on module installation, please visit the detailed CPAN module installation guide.