Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, a module is a reusable collection of related subroutines and variables, usually designed around a common purpose. Modules help to organize and partition your code, making it easier to maintain, understand, and reuse. This tutorial will introduce you to creating and using modules in Perl.
A Perl module is essentially a package saved in a file with a .pm
extension. This file contains a set of functions, variables, and possibly object-oriented methods.
Let's create a simple module, MathOps.pm
, which provides basic mathematical operations:
package MathOps; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(add subtract); sub add { my ($x, $y) = @_; return $x + $y; } sub subtract { my ($x, $y) = @_; return $x - $y; } 1; # Modules must return a true value.
The above module provides two functions: add
and subtract
.
We use Exporter
to make these functions available for import when the module is use
d.
The @EXPORT
array contains the list of functions to be exported.
Always end your module with a true value, typically 1
.
To use the module in your script:
use MathOps; print add(5, 3), "\n"; # Outputs: 8 print subtract(5, 3), "\n"; # Outputs: 2
Ensure that Perl can find MathOps.pm
. One common method is to place it in the same directory as your script or in a directory listed in the @INC
array, which contains paths Perl checks when searching for modules.
The Comprehensive Perl Archive Network (CPAN) is a vast repository of Perl modules. You can easily install modules from CPAN using the cpan
command:
cpan Module::Name
For example:
cpan JSON
Perl comes with a set of core modules that are part of the standard distribution. Here's a simple example using the List::Util
core module:
use List::Util qw(min max); my @numbers = (3, 9, 2, 8, 6); print min(@numbers), "\n"; # Outputs: 2 print max(@numbers), "\n"; # Outputs: 9
Use Strict and Warnings: Always include use strict
and use warnings
in your modules. This helps to catch common mistakes.
Documentation: Use POD (Plain Old Documentation) for module documentation, so users can easily understand how to use your module.
Tests: If you're planning to share your module, consider adding tests. Perl uses a simple testing framework called Test::Simple
.
Perl modules offer an efficient way to encapsulate and reuse code, helping developers to better manage larger codebases. With the combination of custom modules and the vast array of CPAN modules available, Perl offers a powerful and extensible environment for a wide range of tasks.
Creating modules in Perl:
# MyModule.pm package MyModule; sub greet { print "Hello, from MyModule!\n"; } 1; # Required for successful module loading
Using CPAN modules in Perl:
cpan
or cpanm
.use LWP::Simple; my $content = get("https://www.example.com"); print $content if defined $content;
Module import and export in Perl:
Exporter
module is commonly used for this purpose.# MyExportModule.pm package MyExportModule; use Exporter qw(import); our @EXPORT = qw(greet); sub greet { print "Hello, from MyExportModule!\n"; } 1; # Script using the module use MyExportModule; greet();
Installing Perl modules:
cpan
or cpanm
.# Using cpanm cpanm LWP::Simple
Perl standard library modules:
File::Path
), regular expressions (Regexp::Common
), and more.use File::Path; my $dir = "/path/to/create"; mkpath($dir);
Developing custom Perl modules:
# MyCustomModule.pm package MyCustomModule; sub custom_function { # Implementation } 1;
Perl module versioning and dependencies:
{ "name": "MyModule", "version": "1.0", "prereqs": { "runtime": { "requires": { "SomeOtherModule": ">= 2.0" } } } }