Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

perlmod - Perl Modules

Part:   1  2  3  4  

(Continued from previous part...)

Defining and Using Perl Modules

Perl Module - A special source code file that:

  • Contains variables and subroutines related to a single subject.
  • Can be used as an include file.
  • Has its own name space.
  • Has a file name matches the name space name. File extension should be .pm.

Here is a dummy example of Perl module, MyModule.pm,

#- MyModule.pm
#- Copyright (c) 1995 by Dr. Herong Yang
#
   package MyModule;
   sub BEGIN {
      print("Printing in BEGIN of ",__PACKAGE__,"...\n");
      $begin = "Begin";
   }   	
   sub CHECK {
      print("Printing in CHECK of ",__PACKAGE__,"...\n");
      $check = "Check";
   }   	
   sub INIT {
      print("Printing in INIT of ",__PACKAGE__,"...\n");
      $init = "Init";
   }
   sub END {
      print("Printing in END of ",__PACKAGE__,"...\n");
      $end = "End";
   }
   print("Printing from MyModule.pm...\n");
sub myModuleSub {
   print("Printing from myModuleSub()...\n");
}
1;   

And a dummy calling program, ModuleTest.pl,

#- ModuleTest.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
   print("Printing from ModuleTest.pl before MyModule...\n");
   require MyModule;
   print("Printing from ModuleTest.pl after MyModule...\n");
   print("MyModule::begin = $MyModule::begin\n");
   print("MyModule::check = $MyModule::check\n");
   print("MyModule::init = $MyModule::init\n");
   print("MyModule::end = $MyModule::end\n");
   &MyModule::myModuleSub();
   exit;

Output:

Printing from ModuleTest.pl before MyModule...
Printing in BEGIN of MyModule...
Printing from MyModule.pm...
Printing from ModuleTest.pl after MyModule...
MyModule::begin = Begin
MyModule::check =
MyModule::init =
MyModule::end =
Printing from myModuleSub()...
Printing in END of MyModule...

A couple of notes here:

  • If you use bare word in the require function, it will automatically append .pm, and use it as the module file name.
  • The output shows that CHECK() and INIT() were not executed. I don't know why.
  • The output shows that BEGIN() in MyModule.pm was executed during the compilation process of MyModule.pm, but that was in the middle of the execution of the calling program, ModuleTest.pl.
  • The output also shows that END() in MyModule.pm was not executed at the end of the execution process of MyModule.pm. It was executed at the end of the entire execution - end of ModuleTest.pl.

A Simple Sample Module - CalendarModule.pm

I think I am ready to write simple modules now. Here is one that provides calendar elements as scalars, and offers one subroutine to determine if the current year is a leap year:

#- CalendarModule.pm
#- Copyright (c) 1995 by Dr. Herong Yang
#
   package CalendarModule;
   sub BEGIN {
      $author = "Dr. Herong Yang";
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$b) = localtime();
      ($swday,$smon,$smday,$stime,$syear) = split(' ',localtime());
   }
   sub isLeapYear {
      local $day59 = time() - ($yday-59)*24*60*60;
      local ($0,$1,$2,$3,$m,$5,$6,$7,$8) = localtime($day59);
      $m = 0 unless $m==1;
      return $m;
   }
1;

Here is the testing program:

#- CalendarTest.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
   require CalendarModule;
   print("Date: $CalendarModule::smon");
   print(" $CalendarModule::smday");
   print(" $CalendarModule::syear\n");
   print("Leap year? ",&CalendarModule::isLeapYear(), "\n");
   exit;

Output looks good to me:

Date: Aug 13 1995
Leap year? 0

Part:   1  2  3  4  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - perlmod - Perl Modules