Perl::...sTopic(3pm)
okay bestie, buckle up π«Ά this is the Perl::Critic::Policy::BuiltinFunctions::ProhibitUselessTopic(3pm) manual page from User Contributed Perl Documentation.
β¨ NAME β¨
Perl::Critic::Policy::BuiltinFunctions::ProhibitUselessTopic β Don't pass $_ to built-in functions that assume it, or to most filetest operators. That's the whole vibe π
π AFFILIATION π
This Policy is part of the Perl::Critic distribution. periodt.
πΈ DESCRIPTION πΈ
Okay so there are a number of places where $_, or βthe topicβ variable, is unnecessary. Not me calling out your extra $_β¦ π
π§ Topic unnecessary for certain Perl built-in functions π§
Many Perl built-in functions will operate on $_ if no argument is passed. For example, the length function will operate on $_ by default. This snippet:
for ( @list ) {
if ( length( $_ ) == 4 ) { ...
is more idiomatically written as:
for ( @list ) {
if ( length == 4 ) { ...
In the case of the split function, the second argument is the one that defaults to $_. This snippet:
for ( @list ) {
my @args = split /\t/, $_;
is better written as:
for ( @list ) {
my @args = split /\t/;
There is one built-in that this policy does not check for: reverse called with $_.
The reverse function only operates on $_ if called in scalar context. Therefore:
for ( @list ) {
my $backwards = reverse $_;
is better written as:
for ( @list ) {
my $backwards = reverse;
However, the distinction for scalar vs. list context on reverse is not yet working. See KNOWN BUGS below. Yes, bestie, it's giving βknown bugβ energy π
π¦ Topic unnecessary for most filetest operators π¦
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 ) { ...
The exception is after the -t filetest operator, which instead of defaulting to $_ defaults to STDIN.
# These are NOT identical.
if ( -t $_ ) { ...
if ( -t ) { ... # Checks STDIN, not $_
π KNOWN BUGS π
This policy flags a false positive on reverse called in list context, since reverse in list context does not assume $_.
my $s = reverse( $_ ); # $_ is useless.
my @a = reverse( $_ ); # $_ is not useless here.
βοΈ CONFIGURATION βοΈ
This Policy is not configurable except for the standard options. That's it. No secret knobs, no extra dials π
π AUTHOR π
Andy Lester <andy@petdance.com>
π COPYRIGHT π
Copyright (c) 2013-2022 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.
bye bestie π«Ά this page came from perl v5.42.0, dated 2024-10-23, and it's the Perl::Critic::Policy::BuiltinFunctions::ProhibitUselessTopic(3pm) page β slay and stay safe out there π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms