...
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 behavior of 'sort()
' is undefined."
Noncompliant Code Example (sort()
)
...
Code Block | ||||
---|---|---|---|---|
| ||||
sub ret {
my $list = shift;
my @list = @{$list};
# ...
return sort @list;
}
my @list = ( "foo", "bar", "baz");
my $result = ret @list;
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
sub ret {
my $list = shift;
my @list = @{$list};
# ...
return sort @list;
}
my @list = ( "foo", "bar", "baz");
my @result = ret @list;
|
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 Medium | unlikely Unlikely | low Low | P2 P6 | L3 L2 |
Automated Detection
Tool | Diagnostic |
---|---|
Perl::Critic | Subroutines::ProhibitReturnSort |
Bibliography
...
...
[ |
---|
...
...
...
|AA. Bibliography#Conway 2005]\] \[[Manpages|AA. Bibliography#Manpages]\] [perlfunc|http://perldoc.perl.org/perlfunc.html]EXP11-C. Do not apply operators expecting one type to data of an incompatible type 03. Expressions (EXP) EXP13-C. Treat relational and equality operators as if they were nonassociative