Professional Perl | 6
[previous] [next] |
Professional Perl
Strict Subroutines and the 'use strict subs' Pragma
The strict pragma has three components, refs, vars, and subs. The subs component affects how Perl interprets unqualified (that is, not quoted or otherwise identified by the syntax) words or 'barewords' when it encounters them in the code.
Without strict subroutines in effect, Perl will allow a bareword and will interpret it as if it were in single quotes:
$a = bareword;
print $a; #prints "bareword";
The problem with this code is that we might later add a subroutine called bareword, at which point the above code suddenly turns into a function call. Indeed, if we have warnings enabled, we will get a warning to that effect:
Unquoted string "bareword" may clash with future reserved word at ...
Strict subroutines are intended to prevent us from using barewords in a context where they are ambiguous and could be confused with subroutines. To enable them, use one of the following:
use strict; # enables strict refs, vars, and subs
use strict subs; # enables strict subs only
Now any attempt to use a bareword will cause Perl to generate a fatal error:
Bareword "bareword" not allowed while "strict subs" in use at ...
Ironically, the second example contains the illegal bareword subs. It works because at the point Perl parses the pragma it is not yet in effect. Immediately afterwards, barewords are not permitted, so to switch off strict subs again we would have to use either quotes or a quoting operator like qw:
no strict 'subs';
no strict q(subs);
no strict qw(subs);
Predeclaring Subroutines
Perl allows subroutines to be called in two alternate syntaxes; functions with parentheses or list operators. This allows subroutines to be used as if they were one of Perl's built-in list operator functions such as print or read (neither of which require parentheses).This syntax is only valid if Perl has already either seen the subroutine definition or a declaration of the subroutine. The following subroutine call is not legal, because the subroutine has not yet been defined:
debug "This is a debug message"; # ERROR: no parentheses
#...rest of program...
sub debug {
print STDERR @_, "\n";
}
The intention here is to create a special debug statement, which works just like the print statement, but prints to standard error rather than standard out, and automatically adds a linefeed. Because we want it to work like print in all other respects we would prefer to omit the brackets if we choose to, since print allows us to do that.
# predeclare subroutine 'debug'
sub debug;
debug "This is a debug message"; # no error
#...rest of program...
sub debug {
print STDERR @_, "\n";
}
Subroutines are also predeclared if we import them from another package (see Chapter 10 for more on packages), as in:
use mypackage qw(mysubroutine);
It is worth noting here that even if a package automatically exports a subroutine when it is used, that does not predeclare the subroutine itself. In order for the subroutine to be predeclared, we must name it in the use statement. Keeping this in mind, we might prefer just to stick to parentheses.
[previous] [next] |
Created: March 8, 2001
Revised: March 8, 2001