Previous Page Next Page

A.3. Perl Pragmas

A pragma is a special "pseudo" module that hints how the compiler should behave. The use declaration allows the importation of compiler directives called pragmas into your Perl program. Pragmas determine how a block of statements will be compiled. They are lexically scoped; the scope is limited to the current enclosing block and can be turned off with the no directive. Pragma names are conventionally lowercase. Table A.5 is a partial list.

Table A.5. Perl Pragmas
PragmaWhat It Does
use autouseProvides a mechanism for runtime demand loading of a module only when a function from that module gets called.
use baseLets a programmer declare a derived class based on listed parent classes at compile time and eliminates the need for require;
 e.g., use base qw(A B);is equivalent toBEGIN{ require A; require B;; push(@ISA, qw(A B));}
use bytesPrior to Perl 5.6, all strings were treated as a sequence of bytes. Now strings can contain characters wider than a byte that are represented as numbers. The bytes pragma allows you to specify that the code is using the older, byte-oriented semantics.
use constantDeclares the named symbol to be a constant with a given scalar or list;

For example:

use constant BUFFER_SIZE => 4096;

use constant OS=> 'Solaris';
use diagnosticsForces verbose warning messages beyond the normal diagnostics issued by the Perl compiler and interpreter. Since it affects only the innermost block, the pragma is normally placed at the beginning of the program. Cannot use no diagnostics.
use integerA lexically scoped pragma that tells the compiler to handle all mathematical operations as integer math and truncates the fractional part of floating point numbers when performing such operations.
use localeA lexically scoped pragma that tells the compiler to enable or disable the use of POSIX locales when dealing with regular expressions, built-in operations, character conversions, etc.
use openDeclares one or more default disciplines for I/O operations; the two disciplines currently supported are :raw and :crlf.
use overloadUsed to redefine the meanings of built-in operations when using objects. See Math::BigFloat in the standard Perl library for examples of overloaded operators.
use strict 'vars'With 'vars' as an argument, must use lexical (my) variables or fully qualified variable names with the package name and the scope operator or imported variables. If not adhered to, will cause a compilation error.
use strict 'ref'Generates a runtime error if symbolic references are used, such as typeglobs.
use strict 'subs'Generates a compile-time error if a bareword is used and it is not a predeclared subroutine or filehandle.
use strictGenerates compile-time errors if symbolic references are used, if non-lexical variables are declared, or if barewords that are not subroutines or filehandles are used.
use vars qw(list)Used to declare global variables before our was introduced.
use warningsA lexically scoped pragma that permits flexible control over Perl's built-in warnings like the -w switch or $^W variable.
use lib 'library path'Loads in the library at compile time, not runtime.
use sigtrap 'signal names'Initializes a set of signal handlers for the listed signals. Without an argument for a set of default signals. Prints a stack dump of the program and issues an ABRT signal.
use subs qw(subroutine list)Predeclares a list of subroutines allowing the subroutines listed to be called without parentheses and overrides built-in functions.
no integerTo turn off or unimport the pragma, the pragma name is preceded with no.


 

Previous Page Next Page