Perl::Critic::Policy::Subroutines::ProhibitReturnSort(3pm)

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.

  1. 6.0
  2. 7
  3. 8
  4. 9
  5. 10
  6. 11
  7. 12
  8. 13
  9. Arch

Author

Jeffrey Ryan Thalhammer <[email protected]>

From perl-perl-critic 1.156-3 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.