Rodster001 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings!

I am a little curious as to why this matches the second regex. My thinking is that if it is undef then it should fail. But, apparently that isn't the case. Can someone shed some light? Thanks!

#!/usr/bin/perl use strict; use warnings; no warnings ("uninitialized"); my $id = 1001; if ($id =~ /^\d*$/) { print "Match\n"; } $id = undef; if ($id =~ /^\d*$/) { print "Match\n"; }

Replies are listed 'Best First'.
Re: Uninitialized variable matches regex
by kennethk (Abbot) on Mar 23, 2009 at 20:35 UTC
    The * operator matches zero or more of the linked character type - undef has exactly 0 occurences of digits, and thus matches. Try + to avoid this one. It's all in the Quantifiers section of perlre.

    Update: A little more detail. Your regex has the following three components:

    1. ^ - Match the beginning of the line
    2. \d* - Match a digit character 0 or more times
    3. $ - Match the end of the line (or before newline at the end)

    The act of binding a scalar to the regular expression stringifies it (as Anonymous Monk points out below). That is why your number assignment matches. So once you bind undef (with appropriate warning suppression), undef becomes '', which has a start, 0 digits and an ending, and thus matches.

    As an interesting note, you also match on $id = "\n";, and if you use the m modifier, you match $id = "My favorite number is:\n42\nWhat's yours?";

      Oh, crap... I knew that. Or, at least I did at one time, but it got overwritten. Thanks, man.
Re: Uninitialized variable matches regex
by Anonymous Monk on Mar 23, 2009 at 20:42 UTC
    Further to kennethk's reply, an undefined scalar is coerced to a zero-length string in string context such as that supplied by a regex match.