Exporting and Importing Package Identifiers

This section provides a tutorial example on how to access identifiers from another package as aliases, *identifier = *package::identifier.

Instead of asking all the calling packages to define aliases, Perl offers a solution to help package to export identifiers (symbols) 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, http://www.herongyang.com/
#
   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, http://www.herongyang.com/
#
   use CalendarExported qw($smon $smday $stime $syear &isLeapYear);
   print("Date: $smon $smday $syear\n");
   print("Leap year? ",&isLeapYear(),"\n");
   exit;

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

Typeglob and Importing Identifiers from Other Packages

 Typeglob, Symbolic Table and Identifier Aliases

 Accessing Identifiers from Other Packages as Aliases

Exporting and Importing Package Identifiers

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version