fixed DataForm/diagnose.t

Added find() and findAndLoad() to WebGUI::Pluggable and updated sbin/preload.perl
This commit is contained in:
Doug Bell 2008-10-27 16:16:18 +00:00
parent 0c320d8c01
commit 314035912d
5 changed files with 177 additions and 18 deletions

View file

@ -15,6 +15,7 @@ package WebGUI::Pluggable;
=cut
use strict;
use Module::Find;
use Carp qw(croak);
# Carps should always bypass this package in error reporting
@ -38,12 +39,123 @@ This package provides a standard way of quickly and safely dynamically loading p
my $output = eval { WebGUI::Pluggable::run($module, $function, \@params) };
my @modules
= WebGUI::Pluggable::find( $namespace,
{
exclude => [ $moduleToExclude ],
}
);
my @loadedModules
= WebGUI::Pluggable::findAndLoad( $namespace,
{
onLoadFail => sub { warn "Failed to load " . shift . " because " . shift },
}
);
=head1 FUNCTIONS
These functions are available from this package:
=cut
#----------------------------------------------------------------------------
=head2 find ( namespace, options )
Return an array of all the modules in the given namespace. Will search all
@INC directories. C<options> is a hashref of options with the following keys
exclude => An arrayref of modules to exclude
onelevel => If true, only find sub modules (children), no deeper
find( "CGI", { onelevel => 1 } ) would match "CGI::Session" but
not "CGI::Session::File"
=cut
# TODO: If necessary, use File::Find::Rule instead of Module::Find
sub find {
my $namespace = shift;
my $options = shift;
# Argument sanity
if ( $options && ref $options ne "HASH" ) {
WebGUI::Error::InvalidParam->throw(
error => "Second argument to find() must be hash reference",
);
}
if ( $options->{ exclude } && ref $options->{ exclude } ne "ARRAY" ) {
WebGUI::Error::InvalidParam->throw(
error => "'exclude' option must be array reference"
);
}
my @modules = ();
if ( $options->{ onelevel } ) {
@modules = Module::Find::findsubmod $namespace;
}
else {
@modules = Module::Find::findallmod $namespace;
}
### Remove hidden files
@modules = grep { !/::[.]/ } @modules;
### Exclusions
# Create a hash for quick lookups
if ( $options->{ exclude } ) {
my %modulesHash;
@modulesHash{ @modules } = ( 1 ) x @modules;
delete @modulesHash{ @{ $options->{exclude} } };
@modules = keys %modulesHash;
}
return @modules;
}
#----------------------------------------------------------------------------
=head2 findAndLoad ( namespace, options )
Find modules and load them into memory. Returns an array of modules that are
loaded.
Uses L<find> to find the modules, see L<find> for information on arguments.
Additional options for this method:
onLoadFail = A subroutine to run when a module fails to load, given
the following arguments:
1) The module name
2) The error message from $@
=cut
sub findAndLoad {
my $namespace = shift;
my $options = shift;
my @modules = find( $namespace, $options );
my @loadedModules;
MODULE:
for my $module ( @modules ) {
# Try to load
if (!eval { load( $module ) }) {
if ( $options->{ onLoadFail } ) {
$options->{ onLoadFail }->( $module, $@ );
}
next MODULE;
}
# Module loaded successfully
push @loadedModules, $module;
}
return @loadedModules;
}
#-------------------------------------------------------------------
=head2 instanciate ( module, sub, params )
@ -103,6 +215,8 @@ sub load {
if ($moduleError{$module}) {
croak "Could not load $module because $moduleError{$module}";
}
# Try to load the module
my $modulePath = $module . ".pm";
$modulePath =~ s{::|'}{/}g;
if (eval { require $modulePath; 1 }) {