I have very simple code that parses a file name:
#!/usr/bin/env perl
use 5.040;
use warnings FATAL => 'all';
use autodie ':default';
my $string = '/home/con/bio.data/blastdb/phytophthora.infestans.KR_2_A2/GCA_012552325.1.protein.faa';
if ($string =~ m/blastdb\/(\w)\w+\.([\w\.]+)/) {
my $rest = $2; # $1 would be valid here
$rest =~ s/\./ /g;
my $name = "$1.$rest"; # $1 disappears here
}
the above code fails with Use of uninitialized value $1 in concatenation (.) or string
However, if I save $1
into a variable, e.g. $g
, the information isn't lost.
if ($string =~ m/blastdb\/(\w)\w+\.([\w\.]+)/) {
my ($g, $rest) = ($1, $2);
$rest =~ s/\./ /g;
my $name = "$g.$rest";
}
So I can fix this.
However, $1
shouldn't just disappear like that, shouldn't $1
remain valid while in scope? Is this a bug in Perl? or is there some rule in https://perldoc.perl.org/perlretut that I missed?