PrePAN

Sign in to PrePAN

Module::Modular Create optional plugins for your modules

Good

Synopsis

# MyModule.pm
    
        package MyModule;

        use Module::Modular;
        load_plugins qw<Foo Bar>;
    
        sub load_another_plugin {
            load_plugins 'DifferentOne';
        }

        # MyModule::Plugin::Foo
        package MyModule::Plugin::Foo;

        sub __init {
            my ($class, $name) = @_;
            # $class = MyModule::Plugin::Foo
            # $name  = Foo

            # some code here to be run when loaded
        }

        sub foo {
            print "You have been foo'd!\n";
        }

Description

Module::Modular allows you, or others, to create plugins for your modules. They are not loaded by default - only when you want them to be. This means you can even load them further on down in your module if you wish. The idea is to have your plugins handle certain tasks you don't want cluttering the core code of your module. I started writing this before I came across another plugin module called Module::Pluggable. So if you like how that one works better, or even prefer the name (I do), then go check it out. This one is different in the sense you explicitly tell your module what plugins to load, and each plugin may have an initialiser ("__init") that will get run once it has been loaded, which I found pretty neat. This module is modular itself. By importing "with" followed by an array of options you can extend the functionality of Module::Modular. Currently just the one option is available ("Accessors") which provides methods for accessing meta data of your plugins. A plugin can only be loaded if it's within the same namespace and within your path (ie: YourModule::Plugin::*)

Comments

The "sub __init" does not seem necessary. In a module, any code outside all functions will run when the module is loaded. An alternative is to call the "__init" function "import": the sub would be called when the module is use-d (use MyModule::Plugin::Foo). See "perldoc -f use" for more info.

Anyway, I don't see the value of this module besides Class::Load, Module::Pluggable. Could you tell us how your solution is different/better?
As the POD states, "This one is different in the sense you explicitly tell your module what plugins to load". There may be a way to do a similar thing in Module::Pluggable, but Module::Modular was built around that principle, and I wanted to make it as easy to use as possible. I understand the use of 'import', but the initialiser method this module provides passes two different arguments, the full path name and a short version of the plugin if the author may need it.
I have deliberately mentioned Module::Pluggable in the POD in case people find that suits their needs better.

Please sign up to post a review.