The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

SYNOPSIS

 use Plugin::Loader;

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

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

DESCRIPTION

Note: this module has now been renamed to Module::Loader. In the slim chance that you'd started using Plugin::Loader, my apologies, and please switch to using Module::Loader. Plugin::Loader will eventually be removed from CPAN.

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 Plugin::Loader.

max_depth

Plugin::Loader has an optional max_depth attribute. If you set this to 1, then find_modules will only report modules that are immediately within the namespace specified.

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.

load

Takes a module name and tries to load the module. If loading fails, then we croak.

SEE ALSO

Module::Loader the new name for Plugin::Loader.

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/Plugin-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.