NAME

Module::Loader - finding and loading modules in a given namespace

SYNOPSIS

use Module::Loader;

my $loader  = Module::Loader->new;
my @plugins = $loader->find_modules('MyApp::Plugin');

foreach my $plugin (@plugins) {
   $loader->load($plugin);
}

DESCRIPTION

This module provides methods for finding modules in a given namespace, and then loading them. It is intended for use in situations where you're looking for plugins, and then loading one or more of them.

This module was inspired by Mojo::Loader, which I have used in a number of projects. But some people were wary of requiring Mojolicious just to get a module loader, which prompted me to create Module::Loader.

Note: this module was initially called Plugin::Loader, but I realised that Module::Loader was a more appropriate name.

new

When instantiating Module::Loader, you can optionally set the max_depth attribute, which limits the search depth when looking for modules.

my $loader  = Module::Loader->new(max_depth => 1);

Let's say you have all of the CPAN plugins for the template toolkit installed locally. If you don't specify max_depth, then find_modules('Template::Plugin') would return Template::Plugin::Filter::Minify::JavaScript as well as Template::Plugin::File. If you set max_depth to 1, then you'd get the latter but not the former.

Why might you want to do that?

You might have a convention where plugins are the modules immediately within the specified namespace, but that each plugin can have additional modules within its own namespace.

So typically you'll either not set max_depth, or you'll set it to 1.

METHODS

find_modules

Takes a namespace, and returns all installed modules in that namespace, that were found in @INC. For example:

@plugins = $loader->find_modules('Template::Plugin');

By default this will find all modules in the given namespace, unless you've specified a maximum search depth, as described above. You can also specify max_depth when you call the method:

@plugins = $loader->find_modules('Template::Plugin',
                                 { max_depth => 1 });

This is the same as find_modules() above, but it hard-codes the search depth to 1. This method is provided for compatibility with Mojo::Loader:

@plugins = $loader->search('Template::Plugin');

It just calls find_modules() with max_depth set to 1, as shown above.

load

Takes a module name and tries to load the module.

$loader->load('MyModule::Plugin::Spank');

If loading fails, then we croak.

max_depth

Returns the value of the max_depth, if it was passed to the constructor, otherwise undef.

SEE ALSO

Mojo::Loader was the inspiration for this module, but has a slightly different interface. In particular, it has max_depth hard-coded to 1.

Module::Pluggable is effectively a role which gives a class the ability to find plugins within its namespace.

Module::Pluggable::Ordered is similar to Module::Pluggable, but lets you control the order in which modules are loaded.

all will load all modules in a given namespace, eg with use all 'IO::*';

lib::require::all will load all modules found in a given directory (as opposed to a namespace).

MAD::Loader provides functions for loading modules, but not for finding them.

Module::Find provides a number of functions for finding and loading modules. It provides different functions depending on whether you want to limit the search depth to 1 or not: findallmod vs findsubmod.

Module::Recursive::Require will load all modules in a given namespace, and return a list of the modules found / loaded. It lets you provide regexps for filtering out certain namespaces.

Module::Require provides two functions, require_regex and require_glob which will load all locally installed modules whose name matches a pattern (specified as a regular expression or glob-style pattern).

REPOSITORY

https://github.com/neilbowers/Module-Loader

AUTHOR

Neil Bowers <neilb@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.