logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Affiliation

       This policy is part of Perl::Critic::Community.

Author

       Dan Book, "dbook@cpan.org"

Configuration

       This policy is not configurable except for the standard options.

Description

       Context-sensitive functions, while one way to write functions that DWIM (Do What I Mean), tend to instead
       lead to unexpected behavior when the function is accidentally used in a different context, especially if
       the function's behavior changes significantly based on context. This also can lead to vulnerabilities
       when a function is intended to be used as a scalar, but is used in a list, such as a hash constructor or
       function parameter list. Instead, functions should be explicitly documented to return either a scalar
       value or a list, so there is no potential for confusion or vulnerability.

         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!

Name

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

See Also

       Perl::Critic

perl v5.40.1                                       2025-03-22             Perl::Critic::P...nity::Wantarray(3pm)

See Also