Skip to Content
Perl Cookbook, 2nd Edition
book

Perl Cookbook, 2nd Edition

by Tom Christiansen, Nathan Torkington
August 2003
Intermediate to advanced
964 pages
23h 24m
English
O'Reilly Media, Inc.
Content preview from Perl Cookbook, 2nd Edition

Chapter 10. Subroutines

Composing mortals with immortal fire.

—W. H. Auden, “Three Songs for St Cecilia’s Day”

Introduction

To avoid the dangerous practice of copying and pasting code, larger programs reuse chunks of code as subroutines and functions. We’ll use the terms subroutine and function interchangeably because Perl doesn’t distinguish between the two. Even object-oriented methods are just subroutines that are called using a special syntax, described in Chapter 13.

A subroutine is declared with the sub keyword. Here’s a simple subroutine definition:

sub hello { 
    $greeted++;          # global variable 
    print "hi there!\n";
}

The typical way of calling that subroutine is:

hello( );                 # call subroutine hello with no arguments/parameters

Because Perl compiles your program before executing it, it doesn’t matter where subroutines are declared. Definitions don’t have to be in the same file as your main program. They can be pulled in from other files using the do, require, or use operators, as described in Chapter 12. They can even be created on the fly using eval or AUTOLOAD, or generated using closures, which can act as function templates.

If you are familiar with other programming languages, several characteristics of Perl’s functions may surprise you if you’re unprepared for them. Most recipes in this chapter illustrate how to be aware of—and to take advantage of—these properties.

  • Perl functions have no formal, named parameters, but this is not necessarily a bad thing. See Recipe 10.1 and Recipe ...

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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Perl Cookbook

Perl Cookbook

Tom Christiansen, Nathan Torkington
Learning Perl, 6th Edition

Learning Perl, 6th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 0596003137Errata Page