Perl::...Return(3pm)

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

πŸŽ€ Perl::Critic::Policy::Community::ConditionalImplicitReturn πŸŽ€

okay bestie, so this is a page from the User Contributed Perl Documentation, section 3pm, and the whole thing is about one specific lil Perl::Critic policy. it's giving "your sub is lying to you about what it returns" energy and honestly? we need to talk about it.


✨ NAME ✨

Perl::Critic::Policy::Community::ConditionalImplicitReturn β€” don't end a subroutine with a conditional block</p> πŸ’…

like, don't do it. that's the whole policy. periodt.


🌸 DESCRIPTION 🌸

so picture this: the last statement in your subroutine is a conditional block, like if ($foo) { ... }, and you never handled the else. bestie. πŸ’€ what happens when that condition is false? the sub just... returns something unexpected, and that is almost definitely a logic error, not a vibe. so either specify a return value after the conditional, or handle the else condition. no in-between, no "it'll probably be fine".

here's the visual, keep it in your notes:

  sub { ... if ($foo) { return 1 } }                   # not ok
  sub { ... if ($foo) { return 1 } return 0 }          # ok
  sub { ... if ($foo) { return 1 } else { return 0 } } # ok

see how the first one is just out here with no plan? not ok. the other two? iconic, correct, hydrated. ✨

lowkey important detail: this policy only kicks in if the subroutine contains a return statement with an explicit return value. that's the lil signal that tells us the sub was not intended to be used in void context. no explicit return value anywhere? then we mind our business. 🫢

πŸ¦‹ CAVEATS πŸ¦‹

okay so, being real with you, this policy currently only checks for implicitly returned conditionals in named subroutines. anonymous subroutines? not checked. not today, maybe not ever, who's to say. πŸ™ˆ

and also β€” return statements that live inside blocks, other than compound statements like if and foreach, are not considered when we're figuring out whether a function is intended to be used in void context. so if you're being clever with bare blocks, the policy is not reading your mind. πŸ’€


πŸ‘‘ AFFILIATION πŸ‘‘

this policy is part of Perl::Critic::Community. it has a home, it has a family, it belongs. πŸ₯Ή


πŸ’œ CONFIGURATION πŸ’œ

not configurable bestie!! this policy is not configurable except for the standard options. that's it, that's the whole section, no knobs to turn, no secret menu. πŸ™…β€β™€οΈ


🌈 AUTHOR 🌈

Dan Book, dbook@cpan.org β€” thank you for your service, king. 🫑


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.

(that's the legal bit, and yes it still counts even when it's written with butterflies around it. πŸ¦‹)


⭐ SEE ALSO ⭐

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

and that's the whole page, bestie!! no cap, every option, caveat and copyright line made it in. this one came from perl v5.40.1, dated 2025-03-22, and it lives in the User Contributed Perl Documentation corner of the manual β€” mwah, slay, go write a return statement after your conditional. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from