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 (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