Perl has a large number of builtin functions, they are described on the {{perlfunc}} manpage \[[Wall 2011|AA. Bibliography#Manpages]\]. Perl also has a handful of reserved keywords such as {{while}}; they are described on the {{perlsyn}} manpage \[[Wall 2011|AA. Bibliography#Manpages]\]of built-in functions; they are described on the Wiki Markup perlfunc
manpage [Wall 2011]. Perl also has a handful of reserved keywords such as while
; they are described on the perlsyn
manpage [Wall 2011].
Do not use an identifier for a subroutine that has been reserved for a builtin built-in function or keyword.
Noncompliant Code Example
This noncompliant code example codes a subroutine called open()
, which clashes with the open()
builtin built-in function.
Code Block | ||||
---|---|---|---|---|
| ||||
sub open { my ($arg1, $arg2, $arg3) = @_; print "arg1 = $arg1\n"; print "arg2 = $arg2\n"; print "arg3 = $arg3\n"; } open( my FOO$input, "<", "foo.txt"); # What does this do? |
Perl (v5.12.1) actually invokes the builtin built-in open()
rather than the newly crafted subroutine.
...
This compliant solution uses a different name for its subroutine; consequently, it behaves as expected.
Code Block | ||||
---|---|---|---|---|
| ||||
sub my_open { my ($arg1, $arg2, $arg3) = @_; print "arg1 = $arg1\n"; print "arg2 = $arg2\n"; print "arg3 = $arg3\n"; } my_open( my FOO$input, "<", "foo.txt"); |
Exceptions
DCL31-PL-EX0: This rule does not apply to object methods. Object methods are easy for the parser to distinguish from builtin built-in functions or keywords due to because of their distinct syntax.
...
CERT C++ Secure Coding Standard: DCL32-CPP. Do not declare or define a reserved identifier
CERT C Secure Coding Standard: DCL37-C. Do not declare or define a reserved identifier
Risk Assessment
Using reserved keywords can lead to unexpected program behavior and surprising results.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL31-PL | low Low | probable Probable | medium Medium | P2 P4 | L3 |
Automated Detection
Tool | Diagnostic |
---|---|
Perl::Critic | Subroutines::ProhibitBuiltinHomonyms |
Bibliography
...
Related Guidelines
SEI CERT C Coding Standard | DCL37-C. Do not declare or define a reserved identifier |
---|---|
SEI CERT C++ Coding Standard | DCL51-CPP. Do not declare or define a reserved identifier |
Bibliography
[Conway 2005] | "Homonyms," p. 177 |
[CPAN] | Elliot Shank, Perl-Critic-1.116 |
...
...
|http://search.cpan.org/dist/Perl-Critic/lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm] \[[Wall 2011|AA. Bibliography#Manpages]\] [perlfunc|http://perldoc.perl.org/perlfunc.html], [perlsyn|http://perldoc.perl.org/perlsyn.html] 01. Declarations and Initialization DCL32-PL. Every module must return a true value