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

Instead of a script that loads re-usable modules, I want to re-use the script part and change internal functionality. Much like plugins to a web browser work, where the interface is the same but the functionality differs.

My real-world application is that I'm writing test scripts that test my network and servers. These scripts will be %80 the same, including the interface. I'd rather not re-write or cut-and-paste the bits that gather args and format results. I just want to add the bits that run the test and decide the results.

Thanks!
-Pileofrogs

Replies are listed 'Best First'.
Re: How do plugins work?
by diotalevi (Canon) on Nov 11, 2006 at 00:23 UTC
Re: How do plugins work?
by Joost (Canon) on Nov 10, 2006 at 23:52 UTC
    You'll be moving stuff anyway. If you don't want to move the stuff you want to reuse into a module, how about moving the stuff you don't want to reuse into seperate modules and loading them from the script?

    If you want specifics, please elaborate.

    update...

    here's a very simple example:

    #!perl # process input & args... my $plugin_name = 'Some::Module'; eval "use $plugin_name;" or die "can't load module"; my $output = $plugin_name->process($formatted_input); # format output print $formatted_output;
    and this in Some/Module.pm:
    package Some::Module; sub process { my ($package,$formatted_input) = @_; # do stuff; return $output; }
shameless plug for nagios
by f00li5h (Chaplain) on Nov 11, 2006 at 03:49 UTC

    Yet an other not-really-answering-your-question-but-may-be-intersting post, Nagios may be of interest to you (it's perl,which is good) and it uses `plugins' to find out the status of a remote box, then aggregates it all up, with graphs, and the like.

    Oh, and i'm in no way aligned with Nagios, but a sysadmin friend of mine is in love with it

      Nagios wrawks. I used mon for years and years, but finally bit the bullet and gave Nagios a try. I like it a lot and highly recommend it.

      Hi,

      I also recommend it. ;-)

      Regards,

      fmerges at irc.freenode.net

      Heh... This is funny because what I'm writing is a Nagios plugin, or more accurately, a bunch of plugins.

      I've got a home-made protocol for sharing info about a host. It shares a lot of different stuff, like disk usage, what version of dhcpd am I running, or whatever I want. That means my disk-usage nagios plugin will have the exact same user interface, data gathering interface and data output interface as my correct-version-of-dhcpd plugin. The only difference is the bit that checks disk usage, or checks the dhcpd version.

      So, I'm thinking I'll write a script that handles the user interface (command args etc...), data gathering interface (getting data from my home-brew info server) and data output (nagios style output). The pass/fail logic will be handled by plugins.

Re: How do plugins work?
by GrandFather (Saint) on Nov 11, 2006 at 18:37 UTC

    Here's the start of one way to roll up your own plugin manager:

    use strict; use warnings; my %plugins; opendir DIR, "."; # Search for plugins while ($_ = readdir DIR) { next unless m|(plugin_[^\\/]+)\.pm$|; next unless -f $_; my $plugin = $1; next unless require "$plugin.pm"; $plugins{$plugin} = "${plugin}::"; } closedir DIR; # Run the 'run' sub from each plugin found for (keys %plugins) { no strict "refs"; "$plugins{$_}run"->(); } -------- 8< ----------- # plugin_hi.pm use strict; use warnings; package plugin_hi; sub run { print "Hi\n"; } 1; -------- 8< ----------- # plugin_hello.pm use strict; use warnings; package plugin_hello; sub run { print "Hello\n"; } 1;

    Prints:

    Hello Hi

    Plenty of scope for improvement here. Having the plugins provide objects would be a big improvement in many ways for example.


    DWIM is Perl's answer to Gödel
Re: How do plugins work?
by spatterson (Pilgrim) on Nov 14, 2006 at 15:43 UTC
    Modules are the way to do it. You have a core script which always calls the same named functions but loads one of several modules which supply the same function names, so each time you'd just change the 'use' line in your script.

    The main script

    #!/usr/bin/perl -w use strict; package MyPackage; use MyModule1; print testme();
    MyModule1.pm
    package MyPackage; sub testme { return "Test Module 1"; } 1;

    just another cpan module author