I'm getting this error even though I have declared my regex variable.
my $pattern = '(Cat\.\sNo\.\s\d+)';
Later in my code I use then $pattern
.
if ($page =~ /$pattern/)
{
push(@array, $element);
}
But when I run my code it gives me the below error, and continues to run successfully:
Use of uninitialized value in pattern match (m//)
A search on Google for this error seems to point to scenarios where the variable has not been initialised, although in my case it would seem I have initialised it already?
That message is not telling you that $pattern
is uninitialized; it's telling you that $page
is uninitialized. If you're expecting that $page
might be uninitialized, and that's O.K., then you can bypass the warning, and make things clear for future readers of the source-code, by writing this:
if (defined($page) && ($page =~ /$pattern/))
The problem isn't $pattern
- the problem is $element
A quick test will produce the same result:
use strict;
use warnings;
my $pattern = '(Cat\.\sNo\.\s\d+)';
my $element =~ /$pattern/;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With