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

Typeglob and Package Exporter

Part:   1  2  

(Continued from previous part...)

Couple of interesting notes:

  • The first entry in the symbol table defines an empty string key. What's for?
  • Identifier alias assignment statements seem to be processes during the compilation. This is why the symbolic table printed at the beginning of the execution already contains "y" and "b".
  • $b[1] is undefined, because "b" is defined as an alias only for scalar variable.
  • I am using a symbolic reference to pass the hash variable name into a subroutine.

Accessing Variables from Other Packages as Aliases

As we learned earlier, to access variables from other packages is to use their fully qualified names in the format of packageName::variableName. But this format is too long, and not easy to use, see the following 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;

To make this easier, variables from other packages can be introduced into the current package as aliases. Here is how I improved the previous program:

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

Package Exporter

Instead of asking all the calling packages to define aliases, Perl offers a solution to help package to export variables automatically to the calling packages. Perl offers a helping package called Exporter, which can be used to help your package to define aliases into the calling packages.

To prepare a package to export identifiers (symbols), you need to add the following lines:

   package ModuleName;
   require Exporter;
   @ISA = qw(Exporter);
   @EXPORT = qw(...);       # symbols to export by default
   @EXPORT_OK = qw(...);    # symbols to export on request

To import identifiers (symbols) from an included package, you need to use the "use" statement in one of the following formats:

  use ModuleName;           # import default symbols into my package
  use ModuleName qw(...);   # import listed symbols into my package
  use ModuleName ();        # do not import any symbols

Let's see how I improved my old package to use Exporter. Here is the calendar package ready to export identifier:

#- CalendarExported.pm
#- Copyright (c) 1999 by Dr. Herong Yang
#
   package CalendarExported;
   require Exporter;
   @ISA = qw(Exporter);
   @EXPORT = qw($sec $min $hour $mday $mon $year $wday $yday);
   @EXPORT_OK = qw($swday $smon $smday $stime $syear &isLeapYear);
   sub BEGIN {
      $author = "Dr. Herong Yang";
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$x) = 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 a program to show you how to import identifiers with the "use" statements:

#- CalendarImportTest.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
   use CalendarExported qw($smon $smday $stime $syear &isLeapYear);
   print("Date: $smon $smday $syear\n");
   print("Leap year? ",&isLeapYear(),"\n");
   exit;

Part:   1  2  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - Typeglob and Package Exporter