NAME
Perl::Critic::Policy::Bangs::ProhibitUselessTopic - Explicitly checking a regex against $_ is unnecessary
AFFILIATION
This Policy is part of the Perl::Critic::Bangs distribution.
DESCRIPTION
Match or substitution operations are performed against variables, such as:
$x =~ /foo/;
$x =~ s/foo/bar/;
$x =~ tr/a-mn-z/n-za-m/;
If a variable is not specified, the match is against $_
, also known as "the topic".
# These are identical.
/foo/;
$_ =~ /foo/;
# These are identical.
s/foo/bar/;
$_ =~ s/foo/bar/;
# These are identical.
tr/a-mn-z/n-za-m/;
$_ =~ tr/a-mn-z/n-za-m/;
Including the $_ =~
is unnecessary, adds complexity, and is not idiomatic Perl.
Another place that $_
is unnecessary is with a filetest operator.
# These are identical.
my $size = -s $_;
my $size = -s;
# These are identical.
if ( -r $_ ) { ...
if ( -r ) { ...
CONFIGURATION
This Policy is not configurable except for the standard options.
TO DO
I hope to expand this to other places that $_
is not needed, like in a call to split
or a million other functions where $_
is assumed.
AUTHOR
Andy Lester <andy@petdance.com>
COPYRIGHT
Copyright (c) 2013 Andy Lester <andy@petdance.com>
This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.