How to create an elegant and powerful plugin-based system with PERL

When you want to have a PERL application “modularized”, the “traditional” approach is to have the main script referring (and including) all the modules required for the application to run.

The problem with this approach is that – for every module you will be adding or removing – you would need to modify the application with all the possible mistakes that can arise by doing so.

The approach I propose is to have a main script that uses a “plugin” approach where you will be adding/removing “plugins” without the need to ever touch any other portion of the application.

Other benefits are that you can compartmentalize your development between developers and assign to each developer a “module” without he needs to know what the rest of the application does besides how he will need to interface with the core application.

Using proper coding guidelines, using this approach makes possible to develop big sized applications in PERL in an easy and efficient way. Let’s see how it works and how you can make full use of it.

We have the main controlling file. This is the main application file. Call it any name you want.

This application does the following:

  • it scans a directory (in our case we called it “plugins”) which is going to be a sub-directory of the folder where you will be placing this script. This directory will contain plugin files which will have the extension “.pm” (Perl Module);
  • once the plugins are loaded, it’s executed the init subroutine located in each plugin. This is used to load the “hooks” that will be then used to execute the various functions.
  • upon completion of the initialization procedure, the application tests the existence in memory of the hook to execute “something”

One plugin that always have to be in the system is the “” which has to contain the function “&main” that is used as the default execution point OR used in case a hook is not found in the system.

Please note that like in any perl module, the last line of the plugin must contain “return 1;” or it will go in error.

As you notice, in the plugins, the first function is always named

and the name has to be exactly like the file name (minus the extension).

All the functions of the plugin should begin with the name of the plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *