Perl::Critic::Policy::Subroutines::ProhibitReturnSort3pm
Behavior of "sort" is not defined if called in scalar context.
Affiliation
This Policy is part of the core Perl::Critic distribution.
Overview
The behavior of the builtin "sort" function is not defined if called in scalar context. So if you write a subroutine that directly "return"s the result of a "sort" operation, then your code will behave unpredictably if someone calls your subroutine in a scalar context. This Policy emits a violation if the "return" keyword is directly followed by the "sort" function. To safely return a sorted list of values from a subroutine, you should assign the sorted values to a temporary variable first. For example:
sub frobulate {return sort @list; # not ok!my @sorted_list = sort @list;return @sorted_list # OK}
Known bugs
This Policy is not sensitive to the "wantarray" function. So the following code would generate a false violation:
sub frobulate {if (wantarray) {return sort @list;}else{return join @list;}}
Configuration
This Policy is not configurable except for the standard options.
Credits
This Policy was suggested by Ulrich Wisser and the <http://iis.se> team.
Releases
9 releases since Debian 6.0 (Squeeze); the text changed 8 times.