|Syntactic Confectionery Delight|
What does a failed regular expression match actually return?by Anonymous Monk
|on May 20, 2020 at 09:29 UTC||Need Help??|
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
According to the perlop documentation the regular expression match operator (m//) will return true if it finds a match or false if doesn't. Usually where Perl operators return false they do so by either returning an empty string or 0, however this doesn't appear to be the case for failed regular expression matches.
Normally it doesn't matter as Perl being Perl just does what I intended, but earlier today I encountered a bug when assigning the result of regular expression match as part of initialising a hash - the following code exhibits the issue:
Running this code results in:
Eventually I realised that the failed match for the regular expression was somehow tricking the first comma to be evaluated in a scalar context, rather than a list context (note that if the regular expression matches then it returns 1 and the comma is evaluated in a list context). With this information I was able to fix the bug I had, but curiosity lead me to dig deeper into what was happening, so tried the following code:
From running this code it seems that the failed regular expression match is returning something that isn't really a traditional Perl value, but which does evaluate to false in most situations - a sort of "phantom" false value.
Which leaves me with two questions: