dash2 has asked for the wisdom of the Perl Monks concerning the following question:

This is related to a previous question of mine, but I've focussed my problem rather more closely, I hope.

I want to build a script with a nice API for plugins that lets people simply download a plugin, and it starts working automatically. (My end users aren't programmer types. They need stuff to be simple.) The plugins will extend, or replace, functions of the main script.

My current thoughts on architecture are as follows:
- any pluginable functions will call &replace() at their start and &extend() at the end.
- &replace() will search for plugins in the plugin directory. Plugins that want to replace the particular function will respond to the main plugin interface, and replace the main function with their own version.
- &extend() will do something similar, but as the main function has been called already, it will add to functionality rather than replace it.

Now this would work, but does it not kind of suck?

Here's what I'd like to do - the Right Thing, I think.

This would be most cool. I wouldn't have subroutine calls everywhere. I wouldn't have to specify which functions are pluginable. They all could be! And it uses the perl package system so I can use version numbers etc. for dependencies.

My problem is with point 3. Normally we use() a particular package, which then inherits from whatever. Now in this case, it's the reverse: we want to use "whatever package is available that fulfils this role". So if MainPackage is available, we use MainPackage; if MyPlugin is available, we use MyPlugin which inherits from MainPackage.

Now you see the problem? I don't know how to fix it so that the script knows which package to use(). Almost, I want to call the plugin something consistent and have it replace a default "plugin" which would inherit all its methods from the MainPackage ancestor. But this seems like it's going to involve a heavy overhead in resolving method calls by going up the inheritance tree. Plus, what about different plugins replacing different functions - ie multiple inheritance?

I seem to be trying to go against the way packages work. They inherit everything to one descendant from multiple ancestors. I want to hand function calls down to multiple descendants from one ancestor! Is there a better way? Or a neat solution? or even just a clever hack?

cheers
Dave

Replies are listed 'Best First'.
Re: plugins and inheritance
by Corion (Patriarch) on Feb 27, 2001 at 17:36 UTC

    In such cases (extending a framework simply by adding classes and having all changes local to these classes), I use a central registry (either the Windows registry, a singleton object in my program or an external text file). That registry consists of two fields per entry, name and service. The name field is unique to allow identification (this could be the filename of your plug-in), the service field contains the name of the service this module provides (in other contexts, this is called an interface).

    In order to make installation as painless as possible (simply copy a file), you could blindly require() all .pm files in the plug-in directory, and evaluate the &getServiceName() function that every plug-in must export. This also makes deinstallation painless, but it makes each program startup slow, as every plug-in must be loaded, even if it's not used at all.

    This scheme dosen't relieve you of specifying what happens when two plug-ins supply the same service, but the program will only either one of these two plug-ins - here some UI/configuration would be in order.

Re: plugins and inheritance
by jeroenes (Priest) on Feb 27, 2001 at 20:17 UTC
    Hmmm.... maybe you could use a completely different approach. Now you look in an OO way at the 'capabilities' of your packages. I would try a 'functional' approach:

    For this, I would use Exporter. Every plugin loaded (use'd) simply overwrites the main namespace with the new function. It's than up to the plugin to determine if it's right to overwrite or not. You can search the namespace with %::. Additionally, with @EXPORT_OK, the API can determine which names to import.

    If you set up the API like this, the order in which the modules are loaded is important. Therefore, add the name of the plugin when it's downloaded to a config file. Upon restart, load the plugins in the specified order, to achieve the same state.

    For example, if your API contains the function 'my_sub', the package 'plugin.pm' would EXPORT 'my_sub', thereby overwriting the API's sub.

    You can extend this even to OO-perl, methinks.

    Hope this helps,

    Jeroen
    "We are not alone"(FZ)

      beautiful. that's so simple, elegant and obvious I am kicking myself for not thinking of it. yippee. /me kisses jeroenes.
      dave
Re: plugins and inheritance
by tomhukins (Curate) on Feb 27, 2001 at 18:45 UTC

    As Corion already suggested, I'd use require to pull in the modules you need.

    I would ensure plugins are installed in a special directory, outside Perl's normal @INC, which only contains plugins.

    I'd write an AUTOLOAD subroutine in the main code using File::Find to determine the names of all plugin modules (and cache this information to speed up subsequent calls to AUTOLOAD), then require in any module whose name matches the plugin you are looking for.

    I'm not sure how you'd deal with multiple plugins that offer similar functionality, such as Module::Offering::this and Another::Provider_of::this, but then I'm not totally clear on exactly what you want to achieve.

    I hope this is useful. Of course, TIMTOWTDI!

      That sounds rather clever to me. So, my AUTOLOAD routine would check for plugins with the required subroutine, but failing that would just use the default. In effect, all my subroutines would be routed via AUTOLOAD first...

      Yeah, that is a really nice solution - my only worry wd be, will all the AUTOLOAD calls consume a lot of time and CPU? I guess there's no easy way round that anyway. If I want easy plugins, I have to get runtime subroutine lookups.

      thanks a lot
      dave

        If your subroutines don't do much work, or you're calling them recursively, then the overhead of an AUTOLOAD technique might be prohibitive. Otherwise, I wouldn't worry about it.

        I recall Damian Conway's Object Oriented Perl describing a technique to declare subroutines when AUTOLOAD is called, so after the first call there is no AUTOLOAD overhead. If you're concerned enough, you might want to investigate that.

        Update: The syntax for this is:

        *{$name_of_subroutine_to_create} = \&Name::of::existing::subroutine

(tye)Re: plugins and inheritance
by tye (Sage) on Feb 27, 2001 at 20:42 UTC

    If you want the reverse of inheritance, then reverse the inheritance. Have each plug-in do:

    unshift @MainPackageIReplace::ISA, __PACKAGE__;
    I use unshift rather than push since I assume that you want a newly loaded plug-in to override a previously loaded plug-in.

            - tye (but my friends call me "Tye")
      Nice idea, but that wouldn't actually override functions in the main package, would it? E.g.
      package MainOne; sub foo { ...} package NewPlugin; unshift @MainOne::ISA, __PACKAGE__; sub foo { warn "new plugin!" ... }
      Now calls to MainOne::Foo would still go to MainOne itself, right? You could provide new functions with this idea, but not override existing ones.

      dave hj~

Re: plugins and inheritance
by baku (Scribe) on Feb 28, 2001 at 01:32 UTC

    My suggestion would be similar to the 'registry' idea, but rather than listing out the various plug-ins in a central file, which will have to be maintained in some way (RegClean is your friend), you could grab the first x bytes of each file and search for the @ISA and package declarations.

    (You might also want to check the eol characters (\012 vs \015 vs \012\015), depending upon how much you trust your 'potentially naïve end users,' to borrow a Micros0ftian phrase.)

    Depending upon local system configuration, &c., a 'chunk' size of 512 bytes or 4KB might be best. (iirc, 512 byte blocks are +/- standard on most Unices, MacOS, and ?Win/DOS?. 4KB is the size of Linux's read-ahead, and quite likely other OSen, so if you read 1 byte from a file, it's likely that 4KB are cached by the OS, you'd just as well use them.)

    Regardless, parsing the package and @ISA elements in the first "chunk" of the file gives you a simple way to identify the parent class to be replaced, and the name of the class to import (assuming that the filename itself is likely to be truncated, modified, or generally abused.)

      Rather than manually parsing the files consider this snippet. Since perl already maintains a registry of installed modules (ExtUtils::Installed), why write your own?