cub.uanic has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Monks!

I want to know how I can implement plugin system (in Perl, of course ;).
I'm interesting in different techniques: based on directory structure or modules/objects hierarchy, auto-loaded and load on demand etc - all that can have sense.
Please, give me your wisdom about how I can code this, and may be some links with additionl information.

Thanks in advance

--
cub.uanic

Replies are listed 'Best First'.
Re: How to make plugins
by lin0 (Curate) on Dec 20, 2006 at 18:13 UTC
Re: How to make plugins
by friedo (Prior) on Dec 20, 2006 at 18:14 UTC

    I like to use Module::Find. It's a little slow, but if your process is long-running (like a daemon or mod_perl app) it's sufficient. It lets you easily load all modules under a given namespace. Then you can add plugins just by dropping a new module in the right spot.

    You still have to do some work to use the plugins, of course. Either your client code needs to keep track of the modules loaded, or needs to call some initialization method in them, or the modules need to register themselves with the client code when they're loaded.

Re: How to make plugins
by jettero (Monsignor) on Dec 20, 2006 at 18:00 UTC

    I suspect you can get most of what you want with require and perlboot. If that isn't helpful, please tell me how a plugin differs from a perl package and I will refine my answer.

    UPDATE: some (untested) code as requested:

    for my $potential (<plugin_dir/*.pm>) { eval qq( require "$potential" ); if( $@ ) { warn "plugin failed to load: $potential"; } else { if( $potential =~ m|plugin_dir/(.+?)\.pm| ) { my $c = $1; if( eval { $c->can("expected_method1") and $c->can("somethin +g_else") } ) { push @loaded_plugins, $c; } else { warn "$potential failed to load correctly"; } } }

    Although, rather than rolling your own — which I barely started above, you'd be better off looking into some of the posts below. Of particular interest is "Re: How to make plugins."

    -Paul

      Really, I have some ideas in my head.
      But I don't want to reinvent the wheel, and so - give me please more details, if you can (ideally - with some code or prototype)

      --
      cub.uanic
Re: How to make plugins
by pemungkah (Priest) on Dec 20, 2006 at 22:26 UTC
    Take a look at Class::AutoPlug. It lets you take an existing nonpluggable class and turn it into a pluggable one almost painlessly.

    Disclaimer: I wrote it and did a presentation on it at YAPC last year.

Re: How to make plugins
by rinceWind (Monsignor) on Dec 21, 2006 at 10:36 UTC

    I refer you to the Wikipedia entry on plugin. Plugins and extensions are about adding optional functionality to an application.

    I'm interesting in different techniques: based on directory structure or modules/objects hierarchy, auto-loaded and load on demand etc - all that can have sense.

    The concept behind a plugin is that the core application will still work if the plugin has not been installed. What you are talking about is code that has been installed on the target machine, but not loaded into the Perl interpreter. This is provided in the core module autouse, or you can roll your own with AUTOLOAD subs and/or run time require.

    Plugin architectures on the other hand, can and have been done in Perl many times. The concept is relatively painless, owing to the dynamic nature of the language. A CPAN search for plugin shows a variety of modules that use this concept. I draw your attention in particular to Module::Pluggable, and my own Module::Optional as generic tools for plugins in the sense defined in the wikipedia article.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: How to make plugins
by cub.uanic (Acolyte) on Dec 20, 2006 at 19:57 UTC
    Thank you all, Monks, for you answers, and especially to lin0 !
    I got all needed info and even more :)

    --
    cub.uanic