table of contents
Perl::Critic::Policy::Community::OverloadOptions(3pm) | User Contributed Perl Documentation | Perl::Critic::Policy::Community::OverloadOptions(3pm) |
NAME¶
Perl::Critic::Policy::Community::OverloadOptions - Don't use overload without specifying a bool overload and enabling fallback
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
AFFILIATION¶
This policy is part of Perl::Critic::Community.
CONFIGURATION¶
This policy is not configurable except for the standard options.
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.
SEE ALSO¶
Perl::Critic
2022-07-25 | perl v5.40.0 |