| Mojolicious documentation | Contained in the Mojolicious distribution. |
Mojo::Loader - Loader
use Mojo::Loader;
my $loader = Mojo::Loader->new;
my $modules = $loader->search('Some::Namespace');
$loader->load($modules->[0]);
Mojo::Loader is a class loader and plugin framework.
Mojo::Loader inherits all methods from Mojo::Base and implements the following new ones.
load my $e = $loader->load('Foo::Bar');
Load a class and catch exceptions.
Note that classes are checked for a new method to see if they are already
loaded.
if (my $e = $loader->load('Foo::Bar')) {
die "Exception: $e" if ref $e;
}
search my $modules = $loader->search('MyApp::Namespace');
Search for modules in a namespace non-recursively.
$loader->load($_) for @{$loader->search('MyApp::Namespace')};
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
| Mojolicious documentation | Contained in the Mojolicious distribution. |
package Mojo::Loader; use Mojo::Base -base; # "Don't let Krusty's death get you down, boy. # People die all the time, just like that. # Why, you could wake up dead tomorrow! Well, good night." use Carp 'carp'; use File::Basename; use File::Spec; use Mojo::Command; use Mojo::Exception; # "Homer no function beer well without." sub load { my ($self, $module) = @_; # Check module name return 1 if !$module || $module !~ /^[\w\:\']+$/; # Already loaded return if $module->can('new'); # Load unless (eval "require $module; 1") { # Exists my $path = Mojo::Command->class_to_path($module); return 1 if $@ =~ /^Can't locate $path in \@INC/; # Real error return Mojo::Exception->new($@); } undef; } # "This is the worst thing you've ever done. # You say that so often that it lost its meaning." sub search { my ($self, $namespace) = @_; # Scan my $modules = []; my %found; foreach my $directory (exists $INC{'blib.pm'} ? grep {/blib/} @INC : @INC) { my $path = File::Spec->catdir($directory, (split /::/, $namespace)); next unless (-e $path && -d $path); # Get files opendir(my $dir, $path); my @files = grep /\.pm$/, readdir($dir); closedir($dir); # Check files for my $file (@files) { next if -d File::Spec->catfile(File::Spec->splitdir($path), $file); # Module found my $name = File::Basename::fileparse($file, qr/\.pm/); my $class = "$namespace\::$name"; push @$modules, $class unless $found{$class}; $found{$class} ||= 1; } } return unless @$modules; $modules; } 1; __END__