B::Lint complaining about the use of $_

371 views Asked by At

I'm using B::Lint for static code analysis of the Perl code, and I'm confused about why it is complaining about certain lines of my script:

use strict;
use warnings;

my @numbers;

open(my $fh, "<", "phonenumbers.txt");
while(<$fh>) {
    my @num = $_ =~ /(\d+)/g;
    push @numbers, join('',@num);
}
foreach(@numbers) {
    print("$_\n");
}

Besides push @numbers, join('',@num); every line below open(my $fh, "<", "phonenumbers.txt"); is underlined by the B::Lint, most of them with the following description: "Use of $_", which seems somewhat reasonable for me since this practice could make the code less readable.

As someone that just began learning Perl, I thought that after the following changes, at least the whole code in while would no longer be underlined by B::Lint, that was the case for the while code itself, but not for my @num = $line =~ /(\d+)/g;, now reporting: "Implicit match on $_".

use strict;
use warnings;

my @numbers;

open(my $fh, "<", "phonenumbers.txt");
while(my $line = <$fh>) {
    my @num = $line =~ /(\d+)/g;
    push @numbers, join('',@num);
}
foreach(@numbers) {
    print("$_\n");
}

How is $_ being implicitly used on my @num = $line =~ /(\d+)/g;? Is there a way I could avoid being warned by the linter of that implicit match by writing it differently?

0

There are 0 answers