Perl::...tarray(3pm)
π 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 AND LICENSE π
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
- debian trixie: libperl-critic-community-perl 1.0.4-1 Β· Artistic-2.0
- ubuntu resolute: libperl-critic-community-perl 1.0.4-1 Β· Artistic-2.0
- ubuntu stonking: libperl-critic-community-perl 1.0.4-1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms