Skip to Main Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced content levelIntermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

Exporter

Inside your MyModule.pm file:

package MyModule;

use strict;
use Exporter;

our $VERSION = 1.00;           # Or higher…
our @ISA = qw(Exporter);

our @EXPORT      = qw(f1 %h);  # Symbols imported by default.
our @EXPORT_OK   = qw(f2 f3);  # Symbols imported only by request.
our %EXPORT_TAGS = (           # Mappings for :shortcuts.
    a => [qw(f1 f2 f3)],
    b => [qw(f2 %h)],
);

# Your code here.

1;

From a program or another module that makes use of your module:

use MyModule;                  # Import everything in @EXPORT.
use MyModule ();               # Load module, no imports at all.
use MyModule "f1", "f2", "%h"; # Two subs and a variable.
use MyModule qw(:DEFAULT f3);  # All in @EXPORT + one sub.
use MyModule "f4";             # Fatal because f4 not exported.

Whenever anyone invokes a use declaration to load your module, it calls the import method from your module to fetch any symbols it needs into the package of the invoker. Your module (the one doing the exporting) can define the import method any way it pleases, but the standard way is to inherit the method from the Exporter class module. That is what the code above arranges.

The Exporter module serves as a base class for modules that wish to establish their own exports. Oddly, object-oriented modules typically don't use Exporter, since they don't normally export anything (method calls don't need to be exported). However, the Exporter module itself is accessed in an OO fashion because of the @ISA array you installed, as in our example. When another program or module uses your module, the import method ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Perl, 2nd Edition

Mastering Perl, 2nd Edition

brian d foy
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes
Perl in a Nutshell, 2nd Edition

Perl in a Nutshell, 2nd Edition

Nathan Patwardhan, Ellen Siever, Stephen Spainhour

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata