Skip to Content
Intermediate Perl
book

Intermediate Perl

by Randal L. Schwartz, brian d foy, Tom Phoenix
March 2006
Intermediate to advanced
278 pages
6h 49m
English
O'Reilly Media, Inc.
Content preview from Intermediate Perl

Chapter 15. Exporter

In Chapter 3, we showed you how to use modules, some of which pulled functions into the current namespace. Now we’re going to show you how to get your own modules to do that.

What use Is Doing

So, just what does use do? How does the import list come into action? Perl interprets the use list as a particular form of BEGIN block wrapped around a require and a method call. For example, the following two operations are equivalent:

use Island::Plotting::Maps qw( load_map scale_map draw_map );

BEGIN {
  require Island::Plotting::Maps;
  Island::Plotting::Maps->import( qw( load_map scale_map draw_map ) );
}

Let’s break this code down, piece by piece. First, the require is a package-name require, rather than the string-expression require from Chapter 10. The colons are turned into the native directory separator (such as / for Unix-like systems), and the name is suffixed with .pm (for “Perl module”). For this example on a Unix-like system, we end up with:

require "Island/Plotting/Maps.pm";

Recalling the operation of require from earlier, this means Perl looks in the current value of @INC, checking through each directory for a subdirectory named Island that contains a further subdirectory named Plotting that contains the file named Maps.pm.[*] If Perl doesn’t find an appropriate file after looking at all of @INC, the program dies.[] Otherwise, the first file found is read and evaluated. As always with require, the last expression evaluated must be true (or the program dies), ...

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

Perl Debugged

Perl Debugged

Peter Scott, Ed Wright
Think Perl 6

Think Perl 6

Laurent Rosenfeld, Allen B. Downey
Beginning Perl

Beginning Perl

Curtis Ovid Poe
Pro Perl

Pro Perl

Peter Wainwright

Publisher Resources

ISBN: 0596102062Supplemental ContentErrata Page