Skip to Main Content
Learning Perl, 5th Edition
book

Learning Perl, 5th Edition

by Randal L. Schwartz, Tom Phoenix, brian d foy
June 2008
Beginner content levelBeginner
352 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from Learning Perl, 5th Edition

Pragmas

Pragmas are special modules that come with each release of Perl and tell Perl’s internal compiler something about your code. You’ve already used the strict pragma. The pragmas available for your release of Perl should be listed in the perlmodlib manpage.

You use pragmas much like you’d use ordinary modules, with a use directive. Some pragmas are lexically scoped, like lexical (my) variables are, and they therefore apply to the smallest enclosing block or file. Others may apply to the entire program or to the current package. (If you don’t use any packages, the pragmas apply to your entire program.) Pragmas should generally appear near the top of your source code. The documentation for each pragma should tell you how it’s scoped.

The constant Pragma

If you’ve used other languages, you’ve probably seen the ability to declare constants in one way or another. Constants are handy for making a setting just once, near the beginning of a program, but that can easily be updated if the need arises. Perl can do this with the package-scoped constant pragma, which tells the compiler that a given identifier has a constant value, which may thus be optimized wherever it appears. For example:

use constant DEBUGGING => 0;
use constant ONE_YEAR => 365.2425 * 24 * 60 * 60;

if (DEBUGGING) {
  # This code will be optimized away unless DEBUGGING is turned on
  ...
}

The diagnostics Pragma

Perl’s diagnostic messages often seem somewhat cryptic, at least the first time you see them. But you can always look ...

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.
Start your free trial

You might also like

Learning Perl, 6th Edition

Learning Perl, 6th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Beginning Perl

Beginning Perl

Curtis Ovid Poe
Learning Perl 6

Learning Perl 6

brian d foy
Mastering Perl

Mastering Perl

brian d foy

Publisher Resources

ISBN: 9780596520106Supplemental ContentErrata Page