Chapter 1. Modular Programming with Perl
Perl modules are essential to any Perl programmer. They are a great way to organize code into logical collections of interacting parts. They collect useful Perl subroutines and provide them to other programs (and programmers) in an organized and convenient fashion.
This chapter begins with a discussion of the reasons for organizing Perl code into modules. Modules are comparable to subroutines: both organize Perl code in convenient, reusable “chunks.”
Later in this chapter, I’ll introduce a small
module, GeneticCode.pm. This example shows how to
create simple modules, and I’ll give examples of
programs that use this module.
I’ll also demonstrate how to find, install, and use modules taken from the all-important CPAN collection. A familiarity with searching and using CPAN is an essential skill for Perl programmers; it will help you avoid lots of unnecessary work. With CPAN, you can easily find and use code written by excellent programmers and road-tested by the Perl community. Using proven code and writing less of your own, you’ll save time, money, and headaches.
What Is a Module?
A Perl
module
is a library file that uses package declarations to create its own
namespace. Perl modules provide an extra level of protection from
name collisions beyond that provided by my and
use
strict. They also serve as
the basic mechanism for defining object-oriented classes.
Why Perl Modules?
Building a medium- to large-sized program usually requires you to divide ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access