Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of uninitialized value in pattern match (m//)

Tags:

perl

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?

like image 375
kurotsuki Avatar asked Feb 29 '12 03:02

kurotsuki


2 Answers

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/))
like image 114
ruakh Avatar answered Oct 13 '22 02:10

ruakh


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/;
like image 35
Brian Roach Avatar answered Oct 13 '22 01:10

Brian Roach