2.5. Loading Other Module Libraries

A very important capability of the WebTool is the ability to load libraries from other modules. This allows module authors to leverage code already written for use in another module, promoting code re-use and simplifying the WebTool as a whole. The load_module function is provided for this purpose. It must be called from within a BEGIN block and before any other code is run as shown below, or it will throw errors at runtime.

use WebTool;

BEGIN { WebTool::load_module('services'); };

When writing a module, you should review the available modules and see if any of your required functionality is included in their library modules. Code re-use should be a priority to keep the WebTool as lean as possible and prevent introducing unnecessary bugs. Check the libraries of modules related to the action you are performing to see if a related function exists. For example, if your module needed to perform a DNS related task, the named.pm library in the named module would be a logical place to check for a function that performs your desired action.

2.5.1. Stopping and Starting Services

A common use for calling another module is in the loading of the services module to start, stop, and restart services on the system. As an example, the code below will restart the dhcpd server if it is currently running on the system.

use WebTool;

BEGIN { WebTool::load_module('services'); };

my $services = new services;
if ($services->current_state('dhcpd') { $services->restart('dhcpd'); }

2.5.2. User Account Manipulation

Another common task requires the use of the users module to read or change user or group related information. The following example would determine if a specified user exists on the system.

use WebTool;

BEGIN { WebTool::load_module('users'); };

my $users = new users;
my @userlist = $users->users_list();

foreach my $u (@userslist) {
  if ($u->{'name'} eq $testname) { return true; }
}
return false;