Modules and Exporter

Contents
package
One file, one module
Export with Exporter
use and require

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 qw(trim); print My::Text::trim(" hi "), "\n"; # 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 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 My::Text qw(trim); # compile time, imports trim require My::Text; # runtime, no import

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.

Next: CPAN and cpanm

Article author: Arthur Isaev

Related articles
Perl
Perl course: zero to hero in 50 lessons
What is Perl and where it runs
Install Perl and check the version
Your first Perl script
The debugger as a REPL
Scalars and sigils
Numbers
Strings
Interpolation and quoting
Arrays
push, pop, shift and splice
Hashes
keys, values, exists and delete
Scalar and list context
Conditions and comparison
while, for, next and last
foreach and the default variable
Subroutines
Arguments in @_
References
Nested data structures
Regular expressions: match and anchors
Captures and named groups
Substitution with s///
split and join
grep and map
Sort and the Schwartzian transform
Read and write files
File tests and metadata
Directories and paths
die, warn and eval
Modules and Exporter
CPAN and cpanm
Objects with bless
Modern objects with Moo
Testing with Test::More
strict, warnings and diagnostics
Options with Getopt::Long
Environment and %ENV
system, backticks and pipes
One-liners with -n and -p
CSV and TSV processing
JSON with JSON::PP
HTTP clients with HTTP::Tiny
Sockets and IO::Socket
Databases with DBI
Capstone: todo CLI
Capstone: log report
Performance and profiling
Modern Perl tour
Hero roadmap: the whole course on one page
Install Perl on Windows 11
Install Perl on Ubuntu
Install Perl on Rocky Linux
Install Perl on macOS
Install Perl on FreeBSD
Perl in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: