Skip to content

Latest commit

 

History

History
139 lines (93 loc) · 3.86 KB

pragmas.pod

File metadata and controls

139 lines (93 loc) · 3.86 KB

Pragmas

Perl 5's extension mechanism is modules (modules). Most modules provide functions to call or they define classes (moose), but some modules instead influence the behavior of the language itself.

A module which influences the behavior of the compiler is a pragma. By convention, pragmas have lower-case names to differentiate them from other modules. You've heard of some before: strict and warnings, for example.

Pragmas and Scope

A pragma works by exporting specific behavior or information into the enclosing static scope. The scope of a pragma is the same as the scope of a lexical variable. In a way, you can think of lexical variable declaration as a sort of pragma with funny syntax. Pragma scope is clearer with an example:

Just as lexical declarations affect inner scopes, so do pragmas maintain their effects on inner scopes:

Using Pragmas

Pragmas have the same usage mechanism as modules. As with modules, you may specify the desired version number of the pragma and you may pass a list of arguments to the pragma to control its behavior at a finer level:

Within a scope you may disable all or part of a pragma with the no keyword:

Useful Core Pragmas

Perl 5 includes several useful core pragmas:

  • the strict pragma enables compiler checking of symbolic references, the use of barewords, and the declaration of variables

  • the warnings pragma enables optional warnings for deprecated, unintended, and awkward behaviors that are not necessarily errors but may produce unwanted behaviors

  • the utf8 pragma enables the use of the UTF-8 encoding of source code

  • the autodie pragma (new in 5.10.1) enables automatic error checking of system calls and keywords, reducing the need for manual error checking

  • the constant pragma allows you to create compile-time constant values (though see Readonly from the CPAN for an alternative)

  • the vars pragma allows you to declare package global variables, such as $VERSION or those for exporting (exporting) and manual OO (blessed_references)

Several useful pragmas exist on the CPAN as well. Two worth exploring in detail are autobox, which enables object-like behavior for Perl 5's core types (scalars, references, arrays, and hashes) and perl5i, which combines and enables many experimental language extensions into a coherent whole. These two pragmas may not belong yet in your production code without extensive testing and thoughtful consideration, but they demonstrate the power and utility of pragmas.

Is this ending worthwhile? What a downer.

Perl 5.10.0 added a mechanism by which you may more easily write your own pragmas which respect lexical scoping. See perldoc perlpragma for more details.

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3:

A non-empty Z<>

Around line 135:

=end for without matching =begin. (Stack: [empty])