Section B.6.  Pragmas

Table of Contents

B.6. 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.

B.6.1. 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

B.6.2. 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 they mean, and often a little about what's likely to be the problem and how to fix it. You can save yourself the trouble of searching that manpage if you use the diagnostics pragma, which tells Perl to track down and print out the related information for any message. Unlike most pragmas, this one is not intended for everyday use, as it makes your program read the entire perldiag manpage just to get started. This is potentially a significant amount of overhead in terms of time and memory. Use this pragma only when you're debugging and expecting to get an error message you don't yet understand. It affects your entire program. The syntax is:

    use diagnostics;

B.6.3. The lib Pragma

It's nearly always best to install modules in the standard directories, so they're available for everyone, but only the system administrator can do that. If you install your own modules, you'll have to store them in your own directories, so how will Perl know where to find them? That's what the lib pragma is all about. It tells Perl that the given directory is the first place to look for modules. (That means it's also useful for trying out a new release of a given module.) It affects all modules loaded from this point on. The syntax is:

    use lib '/home/rootbeer/experimental';

Use a nonrelative pathname as the argument since there's no telling what the current working directory will be when your program is run. This is especially important for CGI programs (that is, programs run by a web server).

B.6.4. The strict Pragma

You've been using use strict without having to understand that it's a pragma. It's lexically scoped, and it enforces some good programming rules. See its documentation to learn what restrictions are available in your release of Perl. The Alpaca talks about other things that the strict module performs.

B.6.5. The vars Pragma

In the rare case that you need a global variable when use strict is in effect, you may declare it with the vars pragma.[*] This package-scoped pragma tells Perl you are intentionally using one or more global variables:

[*] If your program will never be used with a version of Perl prior to 5.6, you should use the our keyword instead of the vars pragma.

    use strict;
    use vars qw/ $fred $barney /;
    $fred = "This is a global variable, but that's all right.\n";

This is covered in detail in the Alpaca.

B.6.6. The warnings Pragma

Starting in Perl Version 5.6, you may choose to have lexically scoped warnings with the warnings pragma.[Section B.6.  Pragmas] Rather than using the -w option crudely to turn warnings on or off for the entire program at once, you may specify that you want no warnings about undefined values in one section of code, but want other warnings to be available. This signals the maintenance programmer, "I know that this code would produce warnings, but I know what I'm doing anyway." See the documentation for this pragma to learn about the categories of warnings available in your release of Perl.

[Section B.6.  Pragmas] If your program may be used with a version of Perl prior to 5.6, you shouldn't use the warnings pragma.

    Table of Contents
    © 2000- NIV