Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic(3pm)

Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic3pm

Don't use $_ to match against regexes.

Affiliation

This Policy is part of the Perl::Critic distribution.

Overview

It is not necessary to specify the topic variable $_ when matching against a regular expression.

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 $_.

    # 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/;

This applies to negative matching as well.

    # These are identical
    if ( $_ !~ /DEBUG/ ) { ...
    if ( !/DEBUG ) { ...

Including the "$_ =~" or "$_ !~" is unnecessary, adds complexity, and is not idiomatic Perl.

Configuration

This Policy is not configurable except for the standard options.

Releases

7 releases since Debian 8 (Jessie); the text changed 6 times.

  1. 8
  2. 9
  3. 10
  4. 11
  5. 12
  6. 13
  7. Arch

Author

Andy Lester <[email protected]>

From perl-perl-critic 1.156-3 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.