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::OverloadOptions - Don't use overload without specifying a bool overload

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

       The overload module allows an object class to specify behavior for an object used in various operations.
       However, when activated it enables additional behavior by default: it autogenerates overload behavior for
       operators that are not specified, and if it cannot autogenerate an overload for an operator, using that
       operator on the object will throw an exception.

       An autogenerated boolean overload can lead to surprising behavior where an object is considered "false"
       because of another overloaded value. For example, if a class overloads stringification to return the
       object's name, but the object's name is 0, then the object will be considered false due to an
       autogenerated overload using the boolean value of the string. This is rarely desired behavior, and if
       needed, it can be set as an explicit boolean overload.

       Without setting the "fallback" option, any operators that cannot be autogenerated from defined overloads
       will result in an exception when used.  By setting "fallback" to 1, the operator will instead fall back
       to standard behavior as if no overload was defined, which is generally the expected behavior when only
       overloading a few operations.

        use overload '""' => sub { $_[0]->name };                    # not ok
        use overload '""' => sub { $_[0]->name }, bool => sub { 1 }; # not ok
        use overload '""' => sub { $_[0]->name }, fallback => 1;     # not ok
        use overload '""' => sub { $_[0]->name }, bool => sub { 1 }, fallback => 1; # ok

Name

       Perl::Critic::Policy::Community::OverloadOptions - Don't use overload without specifying a bool overload
       and enabling fallback

See Also

       Perl::Critic

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

See Also