E.5. Conditional Loading of Plugins

If your plugin depends on some condition to be successfully loaded, you should include a test for whatever is required in a BEGIN block, and if the conditions are not present, you should call the Perl die function passing a descriptive message telling why the plugin could not be loaded.

For example, here is part of a hypothetical spell-checker plugin that uses the file /usr/share/dict/words file:

Example E.2. Conditional loading of plugins
package spellcheck;
#: Version: 1.0
#: Description: Spell Checker
#: Author: Somebody

BEGIN {
  die("Word file (/usr/share/dict/words) could not be read")
    unless -r /usr/share/dict/words;
}

...

When the user tries to load that plugin, the test will be made. If the file cannot be read (because it does not exist, or the permissiosn are wrong, or for any other reason), die will be called, the plugin will not be loaded, and the given message will be printed in the screen.

E.5.1. Plugins That Require Other Plugins

If your plugin requires some other plugin, you should use the $world->requireplugin function in a BEGIN block. This function will verify if the named plugin is already loaded. If it is, it returns successfully. If it is not loaded, the function will try to load the plugin. If it could be loaded, the function exits successfully. If, however, the plugin could not be loaded (because it was not found, or because it failed some condition), $world->requireplugin will call die, which will abort the loading the plugin that required the other plugin.

As an example, consider the plugin foo, which requires the bar sec_plugin_

Example E.3. A plugin that requires another
package foo;
#: Version: 1.0
#: Description: The Super Frobnicator
#: Author: Somebody

BEGIN {
  $::world->requireplugin('bar');
}

...

If foo was loaded successfully, you can be sure that the bar plugin and its functionality is present. If bar could not be loaded, foo will not be loaded.