Tracks
/
Perl
Perl
/
Syllabus
/
Packages and Modules
Pa

Packages and Modules in Perl

1 exercise

About Packages and Modules

Importing

A module is usually loaded with the use keyword. This will both load and call the import method on it, importing any exported symbols.

Symbols in the package's @EXPORT array will be imported by default. Symbols in the package's @EXPORT_OK array must be imported explicitly.

Before Perl v5.37, a module would have to end in a true value (usually 1) to indicate the module had loaded successfully. This is not necessary when using the module_true feature.

Exporting

For a symbol to be imported, it must first be exported. The Exporter module is included with Perl and is one of the most convenient options for exporting.

Defining the module:

package Foo;

use Exporter ('import');
our @EXPORT_OK = ('bar');

sub bar { ... }

Using the module:

use Foo ('bar');

bar();

Pragma

A pragma is a module which influences some aspect of the behavior of Perl.

The pragmas strict and warnings are commonly recommended, to restrict unsafe constructs and flag potential problems respectively. The strict pragma is enabled along with use v5.12 or higher. The warnings pragma is enabled along with use v5.35 or higher.

The feature pragma allows you to enable newer and experimental Perl features. When the use keyword is given a specific version of Perl, it will enable the feature bundle for that version.

Core Modules

Along with Exporter, Perl has a large variety of core modules available to use. Some examples useful for exercises you may encounter here include List::Util, Time::Piece, and Math::BigRat.

CPAN

In additon to core modules, CPAN has thousands of additional modules created by the Perl community available for installation. The Perl track uses a cpanfile to install a selection of modules which can be used with Exercism's Perl test runner.

Edit via GitHub The link opens in a new window or tab

Learn Packages and Modules

Practicing is locked

Unlock 1 more exercise to practice Packages and Modules