Module::Pluggable::Dependency - order plugins based on inter-plugin dependencies


Module-Pluggable-Dependency documentation Contained in the Module-Pluggable-Dependency distribution.

Index


Code Index:

NAME

Top

Module::Pluggable::Dependency - order plugins based on inter-plugin dependencies

VERSION

Top

This documentation refers to Module::Pluggable::Dependency version 0.0.3

SYNOPSIS

Top

    package MyClass;
    use Module::Pluggable::Dependency;

and then later ...

    use MyClass;
    my $mc = MyClass->new();
    # returns the names of all plugins installed under MyClass::Plugin::*
    # sorted so that plugins are listed after all their dependencies
    my @plugins = $mc->plugins();




DESCRIPTION

Top

Module::Pluggable::Dependency provides a way to run plugins in situations where one plugin depends on running after other plugins run. This module is similar to Module::Pluggable::Ordered but it determines ordering via dependencies instead of precedence levels.

Each plugin may implement an optional depends method which returns a list of the plugins upon which it depends. Module::Pluggable::Dependency makes sure that plugins are returned in an order which meets the dependency hierarchy.

Why would you use this? Let's say you have a series of plugins for caching complex calculations. Some of the plugins base their complex calculations on the calculations of other plugins. You need to be sure that each plugin is only run after it's dependencies have been run. Because the ordering of plugins is specified through dependencies, additional plugins may be added without manually determining the needed precedence levels.

INTERFACE

Top

Module::Pluggable::Dependency should be a drop-in replacement for Module::Pluggable. It accepts all the same options and just modifies the behavior of the plugins sub (or whatever you named it via sub_name).

depends

Any plugin wanting to indicate it's dependence on another plugin should implement a depends method. The method should return a list of class names for the plugins upon which it depends. For example:

    package My::Plugin::Foo;
    sub stuff { ... }

    package My::Plugin::Bar;
    sub stuff { ... }
    sub depends { qw( My::Plugin::Foo ) }

Because My::Plugin::Foo doesn't have a depends method, it's assumed that the plugin has no dependencies. My::Plugin::Bar however, must be run after the "Foo" plugin.

DIAGNOSTICS

Top

Exceptions from Algorithm::Dependency::Ordered are propagated.

CONFIGURATION AND ENVIRONMENT

Top

Module::Pluggable::Dependency requires no configuration files or environment variables.

DEPENDENCIES

Top

INCOMPATIBILITIES

Top

None known. You should even be able to use Module::Pluggable::Dependency at the same time as Module::Pluggable and other derived classes.

BUGS AND LIMITATIONS

Top

Please report any bugs or feature requests to bug-module-pluggable-dependency at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Pluggable-Dependency. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Module::Pluggable::Dependency

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Module-Pluggable-Dependency

* CPAN Ratings

http://cpanratings.perl.org/d/Module-Pluggable-Dependency

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Pluggable-Dependency

* Search CPAN

http://search.cpan.org/dist/Module-Pluggable-Dependency

AUTHOR

Top

Michael Hendricks <michael@ndrix.org>

LICENSE AND COPYRIGHT

Top


Module-Pluggable-Dependency documentation Contained in the Module-Pluggable-Dependency distribution.

package Module::Pluggable::Dependency;

use warnings;
use strict;
use Module::Pluggable ();
use Algorithm::Dependency::Ordered;
use Algorithm::Dependency::Source::HoA;

our $VERSION = '0.0.4';

sub import {
    my $package = shift;

    # let Module::Pluggable install it's normal subroutine
    my %args = @_;
    $args{package} ||= scalar caller;
    $args{require} = 1 if !$args{require} && !$args{instantiate};
    Module::Pluggable->import(%args);

    # wrap Module::Pluggable's sub to sort by dependencies
    my $sub_name = $args{sub_name} || 'plugins';
    my $installed_sub_name = "$args{package}::$sub_name";
    {
        no strict 'refs';
        no warnings 'redefine';
        my $original_sub = \&$installed_sub_name;
        *{$installed_sub_name} = sub {

            # build a dependency hash
            my %deps;
            my %objects;
            for my $plugin ( $original_sub->(@_) ) {
                my $plugin_name = ref($plugin) || $plugin;
                $deps{$plugin_name} = eval { [ $plugin->depends ] } || [];
                $objects{$plugin_name} = $plugin;
            }

            # calculate plugin order based on dependencies
            my $source = Algorithm::Dependency::Source::HoA->new( \%deps );
            my $deps
                = Algorithm::Dependency::Ordered->new( source => $source );
            return @objects{ @{ $deps->schedule_all } };
        };
    }
}

1;

__END__