Chapter 31. Pragmatic Modules

A pragma is a special kind of module that affects the compilation phase of your program. Some pragmatic modules (or pragmata, for short (or pragmas, for shorter)) may also affect the execution phase of your program. Think of these as hints to the compiler. Because they need to be seen at compile time, they'll only work when invoked by a use or a no, because by the time a require or a do is run, compilation is long since over.

By convention, pragma names are written in all lowercase because lowercase module names are reserved for the Perl distribution itself. When writing your own modules, use at least one capital letter in the module name to avoid conflict with pragma names.

Unlike regular modules, most pragmas limit their effects to the rest of the innermost enclosing block from which they were invoked. In other words, they're lexically scoped, just like my variables. Ordinarily, the lexical scope of an outer block covers any inner block embedded within it, but an inner block may countermand a lexically scoped pragma from an outer block by using the no statement:

use strict;
use integer;
{
    no strict 'refs';       # allow symbolic references
    no integer;             # resume floating-point arithmetic
    # ….
}

More so than the other modules Perl ships with, the pragmas form an integral and essential part of the Perl compilation environment. It's hard to use the compiler well if you don't know how to pass hints to it, so we'll put some extra effort into describing pragmas.

Another ...

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.