Today I seem to be very stupid, or I have a very strange problem. Please consider the following code:
use strict;
use warnings;
use warnings::unused;
use warnings FATAL => 'uninitialized';
my ($String, $Pattern);
$Pattern = "pattern";
$String = "pattern\n";
if (($String =~ m/^([^\ ]+).*$/s) &&
defined($1) &&
($1 =~ m/^.*$Pattern$/s)) {
print $1."\n";
}
This code issues the following warning (and then stops due use warnings FATAL => 'uninitialized';):
Use of uninitialized value $1 in concatenation (.) or string at [...]
I really do not understand this because I am testing if it is $1defined (initialized) before using it. The culprit is apparently the last line of the condition (i.e. ($1 =~ m/^.*$Pattern$/s)). If I leave this, the warning will disappear.
But why? In my opinion, this line should just test $1, but not change its value ( $Patternand the rest of this RegEx does not contain parentheses, so $1you should not reassign a possible match).
... , , Perl $1 , RegEx , RegEx . . ?