Perl::...Return(3pm)

πŸ“¦
version 1.0.4 Β· 4 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Perl::Critic::Policy::Community::EmptyReturn β€” bestie, this policy is here to tell you: do not use return with literally zero arguments. That's it, that's the whole vibe. 🚫

(And yes, the manual name is Perl::...Return(3pm) from the User Contributed Perl Documentation, iykyk.)


✨ DESCRIPTION ✨

Okay so context-sensitive functions? The whole "write a function that DWIMs (Do What I Mean)" thing? Lowkey a trap. 😭 When somebody accidentally calls that function in a different context than you meant, things get weird β€” especially if the function's behavior changes a lot depending on context. And ngl, it can straight up become a vulnerability when a function is supposed to be used as a scalar but ends up in a list, like in a hash constructor or a function parameter list. 🫣

Here's the tea: return with no arguments will return either undef or an empty list, depending on context. Girl, pick one. Instead, return the appropriate value explicitly. Periodt. πŸ’…

return;       # not ok
return ();    # ok
return undef; # ok

And here's the classic little disaster, look at this:

sub get_stuff {
  return unless @things;
  return join(' ', @things);
}
my %stuff = (
  one => 1,
  two => get_stuff(), # oops! function returns empty list if @things is empty
  three => 3,
);

The way that two just quietly vanishes from your hash because @things was empty?? Sneaky. Iconic but in a bad way. πŸ’€

But wait β€” empty returns are permitted by this policy if the subroutine contains no explicit return values at all. That's the signal that it's meant to be used in void context, and we respect that. 🫢


🌸 CAVEATS 🌸

Okay bestie, the fine print, and we're reading it, no skimming:

  • This policy currently only checks return statements in named subroutines. Anonymous subroutines are not checked. πŸ™ˆ
  • Also, return statements inside blocks β€” other than compound statements like if and foreach β€” are not considered when determining if a function is intended to be used in void context.
  • Any non-empty return will cause empty returns within the same subroutine to report violations, even though in list context, return and return () are functionally equivalent. So if your function intends to return list context, the recommendation is to explicitly specify an empty list return with return (). πŸŽ€

πŸ’œ AFFILIATION πŸ’œ

This policy is part of Perl::Critic::Community. It's giving community. 🏘️


⭐ CONFIGURATION ⭐

This policy is not configurable except for the standard options. No knobs to turn, no levers to pull. She is what she is. πŸ’…


πŸ¦‹ AUTHOR πŸ¦‹

Dan Book, dbook@cpan.org β€” absolute legend. πŸ‘‘


Copyright 2015, Dan Book.

This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0. 🎨


πŸŽ€ SEE ALSO πŸŽ€

  • Perl::Critic β€” the main character, go read it πŸ’–

No cap, if you were gonna copy-paste return; anywhere today, don't. Return your values explicitly and your hash keys will stay in your hash. 🫢

That's a wrap, bestie! πŸ’• This page came to you from perl v5.40.1, dated 2025-03-22, under Perl::...Return(3pm). Go forth and return things on purpose. ✨

πŸ“¦ where this page comes from