Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is a module?
A module is a library of Perl code that can be included in your Perl program. When you include a Perl module in a program, the functionality of that module is available for you to use inside your own program.
Typically, the module will export its most popular subroutines and variables into your program. You can then use these subroutines and variables just as if they were declared in your program.
Introduction to Perl modules (SW) 10
11
12
where $variable and subroutine are the things you want to export. This form overrides the default form only the variables and subroutines you specify are imported.
Introduction to Perl modules (SW) 13
Example 1: Text::Wrap
Text::Wrap is a simple but very useful module. It's part of the standard module library, which means that it's installed when you install Perl. Text::Wrap contains a subroutine called wrap() (exported by default), which wraps text to form neat paragraphs. wrap() takes three arguments: the indent string for the first line of a paragraph, the indent string for subsequent lines, and the text to be wrapped.
Introduction to Perl modules (SW) 14
Example 1: Text::Wrap
use Text::Wrap; $indent_first = " $indent_subsq = ""; ";
$text = "For the life of me I could never understand why Mr Perkins didn't enjoy his job. He had everything a man could want: a place on the board, an absent boss and a cat named Henry."; print wrap($indent_first, $indent_subsq, $text);
Introduction to Perl modules (SW) 15
Example 1: Text::Wrap
Output:
For the life of me I could never understand why Mr Perkins didn't enjoy his job. He had everything a man could want: a place on the board, an absent boss and a cat named Henry.
16
Example 1: Text::Wrap
Text::Wrap also has a variable called $columns, which determines how many columns my text wraps to. Unlike wrap(), $columns isn't exported from Text::Wrap by default, so I refer to it here using its fully qualified name:
# start of program as before $Text::Wrap::columns = 20; print wrap($indent_first, $indent_subsq, $text);
Introduction to Perl modules (SW) 17
Example 1: Text::Wrap
Output:
For the life of me I could never understand why Mr Perkins didn't enjoy his job. He had everything a man could want: a place on the board, an absent boss and a cat named Henry.
Introduction to Perl modules (SW) 18
19
21
22
Benefit 3: Modularity
As the name suggests, modules are modular! With modules you can parcel bits of code that do specific tasks (e.g. wrapping text) into discrete bundles, which can be re-used elsewhere. If the code needs to be updated you only need to change the module, not dozens of Perl programs.
Introduction to Perl modules (SW) 23
24
and interact with them using methods, which work just like subroutines:
print $alice->gradeAverage();
25
Example 2: Net::FTP
Net::FTP is an object-oriented Perl module that allows you to access FTP servers. As usual, you start by importing the module:
use Net::FTP;
26
Example 2: Net::FTP
Now you can interact with your new object:
$ftp->login("username", "password"); $ftp->cwd("/pub"); $ftp->get("mrperkins.doc"); $ftp->quit();
27
Module documentation
When you install Perl modules, you get access to documentation for that module. To read the documentation for a module, use the perldoc command:
$ perldoc File::Copy
Alternatively, on Unix you can use the man command. On Windows you will have HTML documentation available there's a link in the Start menu.
Introduction to Perl modules (SW) 28
Further reading
For the official low-down on modules:
$ perldoc perlmod
To read in the bath: Learning Perl Objects, References & Modules by Randal L. Schwartz with Tom Phoenix. June 2003 (est.)
Introduction to Perl modules (SW) 29
Summary
Modules are libraries of code that you can insert into your Perl programs Modules save you time and effort, by providing easy access to common tasks Use the perldoc command to get documentation on a Perl module Search CPAN for general-purpose modules
30