NAME
Perl::Critic::Policy::CodeLayout::RequireParensWithBuiltins - Write lc($x // "Default")
instead of lc $x // "Default"
.
DESCRIPTION
String folding is often used in map lookups where missing parentheses may not provide the expected behavior
$LOOKUP{ lc $name // 'Default' }
$LOOKUP{ lc( $name // 'Default' ) }
When $name
is undefined, the first form will lookup the value for ""
(the empty string) and throw warnings from the lc
call. The second form will lookup the value for "default"
. As an alternative approach
$LOOKUP{ lc($name) || 'Default' }
will lookup the value for "default"
when $name
is undefined, but will still throw warnings from lc
.
CONFIGURATION
The priority for configuration is Allow, Required, Permitted.
Allow
Functions in the allow
section can be called with or without parentheses, no restriction. This overrides all other configurations.
[CodeLayout::RequireParensWithBuiltins]
allow = sqrt
Required
Names configured in the require
section always require parentheses, even when called without arguments or inside blocks. EG lc()
or grep {lc($_)}
. This overrides the permit
option.
[CodeLayout::RequireParensWithBuiltins]
require = lc lcfirst uc ucfirst
Required with arguments
Some functions operate on $_
or other defaults and may be used without parameters. If configured in the permit
section, the functions will require parentheses when called with a parameter. EG both grep {defined} ...
and if(defined($x))
are valid, but if(defined $x)
is a violation.
[CodeLayout::RequireParensWithBuiltins]
permit = defined
NOTES
While coding with parentheses can sometimes lead to verbose constructs, a single case without parentheses can lead to invalid data in processing and results. For these functions, the lack of parentheses causes ambiguity so they can be considered necessary. Code maintainability must also support quick insert of defaults and handling of warnings for undefined values, so calls without those mechanisms are likely incorrect from the start.
BUGS
It's possible that some mathematical functions are more natural without parentheses even when followed by lower-precedence operators. The current policy makes no special exemptions for different precedence interpretations for different functions.