The problem is that the $1, $2, etc. variables only get set
if the regular expression matches. /(\d+)/ does NOT match
"Mm", so no assignment occurs. What you need to do is to check
whether there is a match before using the positional variables,
like this:
if ($sss =~ /(\d+)/) {
# use $1 as you wish
}
else {
# don't!
}
Interestingly, your last code snippet will do the right
thing if you remove the part after the
&& (and an extra
opening parenthesis you seem to have there), because
$1 and $2 will only be used when there is a match, and in that case
they will contain either the strings they matched, or
undef
if their respective subexpressions didn't match anything.
--ZZamboni