Hi
-
my $x = "1-6";
-
if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
-
print "SUCCEED \n";
-
} else {
-
print "FAILED \n";
-
}
-
i've got this simple regexp code. when i run it with use strict i get the following warning msg: "Use of uninitialized value in concatenation (.) or string .." at the line of the regexp match pattern. can anybody tell me what did i do wrong ??
thanks,
As Jeff pointed out, the extra $ in the regexp is causing the problem. The perl variable $\ is the output record seperator which has a value of undef by default. Thus you get the "use of uninitialized..." warning. Do this and the warning goes away:
- use strict;
-
use warnings;
-
$\ = "";# <-- $\ is now initialized as an empty string
-
my $x = "1-6";
-
if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
-
print "SUCCEED \n";
-
} else {
-
print "FAILED \n";
-
}
-
but of course the regexp is then looking for s* (zero or more 's') instead of \s* (zero or more spaces) but because there are no 's' characters after the digits the regexp still matches. I suspect you just want to remove that '$' as Jeff has recommended.