Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

CalendarModule.pm - A Sample Perl Module

This section provides a tutorial sample Perl module, CalendarModule.pm, which hold some calendar elements of the current date and time. It also provides a subroutine, isLeapYear().

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, http://www.herongyang.com/
#
   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, http://www.herongyang.com/
#
   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

Sections in This Chapter

Including Script Codes from Other Files

do() Function - Including Script Files

require() Function - Including Script Files

"package" Statement - Switching Name Space

BEGIN(), CHECK(), INIT() and END() Functions

Defining Your Own Perl Module

CalendarModule.pm - A Sample Perl Module

Dr. Herong Yang, updated in 2008
CalendarModule.pm - A Sample Perl Module