Pragmas

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

You use pragmas with a use directive, much like you’d use ordinary modules. Some pragmas are lexically scoped, like lexical (my) variables are, and so 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 usually 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 once, near the beginning of a program, but that can 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 be optimized wherever it appears as in this 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 cryptic, at least the first time you see them. You can always look them up in the perldiag manpage to find what ...

Get Learning Perl, Fourth 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.