...
Some functions do not define what they return in list or scalar context. For instance, according to the {[perlfunc}} manpage, the sort()
function "sorts the LIST and returns the sorted list value. In scalar context, the behaviour of "sort()" is undefined.".
Noncompliant Code Example (sort()
)
This noncompliant code example inadvertantly assigns a scalar to the result of the sort()
function.
...
The contents of $result
are undefined, because the sort()
function's return value is not defined in a scalar context.
Compliant Solution (sort()
)
This compliant solution guarantees that the ret()
function is only called in list context.
...
In this case, the @result
array will contain the list {{{ "bar", "baz", "foo" }}}.
Risk Assessment
Using an unspecified value can lead to erratic program behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP33-PL | medium | unlikely | low | P2 | L3 |
Automated Detection
Tool | Diagnostic |
---|---|
Perl::Critic | Subroutines::ProhibitReturnSort |
Bibliography
Wiki Markup |
---|
\[[Conway 2005|AA. Bibliography#Conway 2005]\] \[[Manpages|AA. Bibliography#Manpages]\] [perlfunc|http://perldoc.perl.org/perlfunc.html] |
...