Modules and Exporter
package
A package is a namespace for subroutines and variables. Declare one at the top of a file and every name below it belongs to that package until the next declaration.
package My::Text; use strict; use warnings; sub trim { my ($s) = @_; $s =~ s/^\s+|\s+$//g; return $s; } 1;
Two colons separate the parts of a package name, and each part is capitalized by convention. The final 1; matters: a module must return a true value, because use checks the result.
One file, one module
The package name decides the file path. My::Text lives in My/Text.pm, directly under a library folder that the program adds to its search path.
lib/My/Text.pm
use lib "lib"; use My::Text; print My::Text::trim(" hi "), "\n"; # hi
perl demo.pl
hi
Without an export list the full name My::Text::trim is required. That is explicit and always works, but the shorter form below reads better in real code.
Export with Exporter
Exporter is in the core and gives a module an import method. List the public functions in @EXPORT_OK, which means the caller may ask for them by name, and put private helpers outside the list.
package My::Text; use strict; use warnings; use Exporter 'import'; our @EXPORT_OK = qw(trim); sub trim { my ($s) = @_; $s =~ s/^\s+|\s+$//g; return $s; } 1;
With that in place the caller writes use My::Text qw(trim); and then trim($s). The explicit empty list use My::Text (); imports nothing and keeps every name qualified.
use lib "lib"; use My::Text qw(trim); print trim(" hi "), "\n";
perl demo.pl
hi
use and require
use runs at compile time and calls import; require runs at runtime and only loads the file. Practically every module wants use, because imports must exist before the rest of the file is compiled.
use List::Util qw(sum); # compile time: the file loads and sum is imported print "sum: ", sum(1 .. 4), "\n"; print "Math::BigInt loaded: ", exists $INC{"Math/BigInt.pm"} ? "yes" : "no", "\n"; require Math::BigInt; # runtime: the file loads when this line runs print "Math::BigInt loaded: ", exists $INC{"Math/BigInt.pm"} ? "yes" : "no", "\n";
perl demo.pl
sum: 10
Math::BigInt loaded: no
Math::BigInt loaded: yes
The program names two core modules so that it runs on its own; the timing is the same for My::Text above. The first line is loaded before the program starts, the require line inside the program itself.
For code that may not be installed, wrap a require in eval and check $@. That is how a script offers an optional feature without making the module a hard dependency.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a Perl package?
Answer
A namespace for subroutines and variables
A package gives its names a prefix, and every name below the declaration belongs to it until the next package line.
-
A module declares
package My::Text;. In which file does Perl look for it?Answer
My/Text.pm
Each part of the name becomes a folder and the last part becomes the file name, so the colons turn into slashes.
-
Why does a module end with
1;?Answer
It returns a true value, because loading a file that returns false is an error
useandrequireboth check the value the file returns; a module that ends with a false value fails to load. -
What does a name in
@EXPORT_OKmean?Answer
The caller may import it by asking for it by name
Names in
@EXPORTcome in by default, while@EXPORT_OKnames must be listed in the caller's import list. -
What does this program print?
package Greet; use Exporter 'import'; our @EXPORT_OK = qw(hello); sub hello { return "hello, $_[0]" } package main; Greet->import('hello'); print hello("perl"), "\n";Answer
hello, perl
The import puts hello into main, and $_[0] is the argument the caller passed.
-
What does this program print?
package Counter; our $count = 0; sub bump { $count++ } package main; Counter::bump(); Counter::bump(); print $Counter::count, "\n";Answer
2
The package variable is shared, so both calls change the same $count and the full name reaches it from main.
-
What does this program print?
package My::Text; sub trim { my ($s) = @_; $s =~ s/^\s+|\s+$//g; return $s } package main; print My::Text::trim(" hi "), "\n";Answer
hi
The full name needs no import, and the substitution removes the spaces on both sides.
-
What does this program print?
use List::Util qw(sum); print sum(1 .. 4), "\n";Answer
10
The import brings sum into the program, and the range 1 .. 4 adds up to 10.
-
What does this program print?
package A; our $name = "a"; package B; our $name = "b"; package main; print $A::name, $B::name, "\n";Answer
ab
The two package variables are different variables, and the program prints them next to each other with no separator.
-
Which one loads a module when the line runs, rather than before the program starts?
Answer
requireuseis a BEGIN block around a require and an import, while a bare require runs at the point where it stands.
Article author: Arthur Isaev