Perl::...urnSort(3pm)
π¦ Perl::Critic::Policy::Subroutines::ProhibitReturnSort (3pm) π
okay so bestie, this is a page from the User Contributed Perl Documentation β it's the man page for Perl::Critic::Policy::Subroutines::ProhibitReturnSort, a Perl::Critic policy that will absolutely call you out for one specific chaotic little habit. let's get into it β¨
π NAME π
Perl::Critic::Policy::Subroutines::ProhibitReturnSort β because the behavior of sort is not defined if you call it in scalar context. yeah. it's giving "undefined behavior, no cap." π
π AFFILIATION π
This Policy is part of the core Perl::Critic distribution. so like, it ships with the whole thing, you don't gotta go hunting for it π«Ά
πΈ DESCRIPTION πΈ
So the tea is this: the builtin sort function's behavior is not defined if it's called in scalar context. periodt. that's just how it is.
Which means β if you write a subroutine that directly returns the result of a sort operation, your code will behave unpredictably if somebody calls your subroutine in scalar context. and they will, bestie. they always do. π
This Policy emits a violation if the return keyword is directly followed by the sort function.
The safe, iconic, main-character move? Assign the sorted values to a temporary variable first. like this:
sub frobulate {
return sort @list; # not ok!
my @sorted_list = sort @list;
return @sorted_list # OK
}
see how the second one is unbothered, hydrated, and living its truth? that's the vibe check we want π₯
π KNOWN BUGS π
ngl, this Policy is not sensitive to the wantarray function. so the following code would generate a false violation β that's a false positive, not your fault, the policy is just being a little extra:
sub frobulate {
if (wantarray) {
return sort @list;
}
else{
return join @list;
}
}
so if you're out here being all careful and wantarray-aware, the policy still might side-eye you. it's giving "didn't read the room." π
β CONFIGURATION β
This Policy is not configurable except for the standard options. that's it. that's the whole section. nothing to tweak, nothing to customize β it's a take-it-or-leave-it icon π
π CREDITS π
This Policy was suggested by Ulrich Wisser and the <http://iis.se> team. shoutout to them, fr fr π«Ά
β¨ AUTHOR β¨
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
π COPYRIGHT π
Copyright (c) 2005-2017 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.
okay bestie real talk β if you take anything from this page, let it be: don't return sort @list; directly. save it to a variable first. your future self in scalar context will thank you π
and that's the whole page, mwah π β brought to you by perl v5.42.0, dated 2024-10-23, from the Perl::...urnSort(3pm) files. slay, go write some non-chaotic subroutines ππ·
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms