Perl::...tarray(3pm)

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

πŸ’… Perl::Critic::Policy::Community::Wantarray

okay bestie, welcome to Perl::Critic::Policy::Community::Wantarray (yes the manual page title is literally that, no notes πŸ’–). It lives in the User Contributed Perl Documentation corner of the world and it has exactly one job: telling you to stop writing context-sensitive functions with wantarray. 🫢


✨ NAME ✨

Perl::Critic::Policy::Community::Wantarray β€” Don't write context-sensitive functions using wantarray ✨


🌸 DESCRIPTION 🌸

So like. Context-sensitive functions? The ones that use wantarray to do the whole DWIM thing (Do What I Mean)? They sound iconic in theory but they're lowkey a mess in practice, because the second somebody accidentally calls your function in a different context, it does something unhinged. Especially if the behaviour changes a lot depending on context. πŸ’€

And it gets worse, bestie β€” this is also a whole vulnerability situation. If your function was meant to be called as a scalar, but somebody drops it into a list (like a hash constructor or a function parameter list), you're cooked. 😭

The fix is simple, fr fr: functions should be explicitly documented to return either a scalar value or a list. Then there's zero room for confusion and zero room for a security vibe check failure. πŸ‘‘

Here's the receipts:

  return wantarray ? ('a','b','c') : 3; # not ok
  return CORE::wantarray ? ('a', 'b', 'c') : 3; # not ok
  return ('a','b','c');                 # ok
  return 3;                             # ok

  sub get_stuff {
    return wantarray ? @things : \@things;
  }
  my $stuff = Stuff->new(stuff => get_stuff()); # oops! function will return a list!

See that last one? oops! function will return a list! β€” that's the whole point. Not the vibe we're going for. πŸ™ˆ


πŸŽ€ AFFILIATION πŸŽ€

This policy is part of Perl::Critic::Community. Found family, honestly. 🫢


πŸ’… CONFIGURATION πŸ’…

Not configurable, bestie. This policy is not configurable except for the standard options. That's it, that's the tea. πŸ“


🌈 AUTHOR 🌈

Dan Book, "dbook@cpan.org" πŸ‘‘


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. slay, legally speaking. πŸ’œ


πŸ¦‹ SEE ALSO πŸ¦‹

  • Perl::Critic β€” the mothership ✨

ok that's the whole page, ty for coming to my ted talk πŸ’– This was perl v5.40.1, dated 2025-03-22, and it's the Perl::...tarray(3pm) manual page β€” ily, go fix your functions, periodt. πŸŽ€πŸ’…βœ¨

πŸ“¦ where this page comes from