Perl has a large number of punctuation variables. They control the behavior of various operations in the Perl interpreter. While Although they are initially set to reasonable default values, any Perl code has the ability to change their values for its own internal purposes. If a program modifies one of these variables, it is obligated to reset the variable back to its default value, lest it alter the behavior of subsequent unrelated code. The easiest way for a program to "clean up after itself" is to declare such variables local
when modifying them.
...
This program produces the correct result, but it leaves the $/
variable set to an unusual value (:
). Subsequent reads of any file will use this character as the end-of-line delimiter , rather than the typical newline, which is the default value.
...
Code Block | ||||
---|---|---|---|---|
| ||||
sub count_virtual_users { my $result = 0; local $/ = ":"; open( PASSWD, "<", "/etc/passwd"); while (<PASSWD>) { @items = split "\n"; foreach (@items) { if ($_ eq "/usr/bin/false") { $result++; } } } $result; } |
Exceptions
DCL02-PL-EX0: The following global variables may be modified without being declared local
:
...
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL02-PL | lowLow | probableProbable | mediumMedium | P2P4 | L3 |
Automated Detection
Tool | Diagnostic |
---|---|
Perl::Critic | Variables::RequireLocalizedPunctuationVars |
Bibliography
[CPAN] | Elliot Shank, Perl-Critic-1.116 Variables::RequireLocalizedPunctuationVars |
[Wall 2011] | perlfunc, perlvar |
...
01. Declarations and Initialization DCL32-PL. Every module must return a true value