A Perl module template (template code for a Perl module)

If you've ever needed some template/boilerplate code for creating your own Perl module, here's some sample code intended to give you just what you wanted. Feel free to copy and paste the Perl code shown below and use it as a template for creating your own modules.

How to use this sample Perl module

If you've never created your own Perl module I recommend first seeing how this sample code works. Here's all you have to do to get it running:

  1. Wherever you're working, create a subdirectory named DevDaily.
  2. Move into that directory, then copy and paste the contents shown below into a file named Time.pm.
  3. Save that file, and move back up to your original directory.
  4. In your original directory create a file named driver.pl, and paste the contents of my "driver" program shown further below into that file.
  5. Run the driver program something like this:
perl driver.pl

If your environment is configured "normally" -- meaning that Perl is installed and will look in your current directory for modules -- this should work fine.

The DevDaily::Time Perl module template

Here's my Perl module "template". I created this module recently when I needed to format some time in a more human-readable format. As mentioned, once you get this working on your system, feel free to modify this code to create your own Perl modules.

#--------------------------------------------------------------
# The DevDaily::Time Perl module.
# Free to use as a template for creating your own Perl modules.
#--------------------------------------------------------------
package DevDaily::Time;
require Exporter;

our @ISA     = qw(Exporter);
our @EXPORT  = qw(get_time_suffix);   # symbols to be exported by default (space-separated)
our $VERSION = 1.00;                  # version number

use POSIX 'strftime';

#----------------
# get_time_suffix
#----------------

# returns a string formatted like "2008.08.18.12.31.56"
# requires "use POSIX 'strftime';"
sub get_time_suffix
{
  return strftime( '%Y.%m.%d.%H.%M.%S', localtime(time()) );
}

1;

As you might guess from the code, the first part of the package name (the DevDaily part) corresponds to the subdirectory you created, and the Time part corresponds to the Time.pm file you created.

A lot of the other code is boilerplate Perl code that's needed when creating a module. Possibly the most important thing to remember about it is that when you create a new Perl function you need to include it in the EXPORT statement. For instance, if I created a new function called getdayof_week, I'd need to export it like this:

our @EXPORT  = qw(get_time_suffix get_day_of_week);

Fortunately it's actually a pretty easy problem to debug: if you don't export your function you won't be able to access it from the program that tries to call it.

The "driver" program

And finally, here's the code for the "driver" program that you can use to test the DevDaily::Time Perl module shown above:

use DevDaily::Time;

print &get_time_suffix();

As you can see, this driver program is pretty simple, all you have to say is that you want to "use" my Time module, and then you can use the method that I defined in that module. Sure makes your driver program look clean and simple, doesn't it? :)