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:

use Data::Dumper; warn Dumper( { a => "a" =~ m/b/, b => 'asdf' } );

Running this code results in:

$VAR1 = { 'asdf' => undef, 'a' => 'b' };

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:

use Data::Dumper; print Dumper( "a" =~ m/a/ ); # outputs 1 print Dumper( "a" =~ m/b/ ); # No Output For This Line! print Dumper( "a" eq "a" ); # outputs 1 print Dumper( "a" eq "b" ); # outputs an empty string ('') print Dumper( "a" !~ m/a/ ); # outputs an empty string ('') print Dumper( "a" !~ m/b/ ); # outputs 1 my $a = "a" =~ m/a/; print Dumper( $a ); # outputs 1 my $b = "a" =~ m/b/; print Dumper( $b ); # outputs an empty string ('')

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:

  1. What is the actual value returned by a failed regular expression match?
  2. Why does =~ return this "phantom" version of false, while !~ returns the more common empty string version?

In reply to What does a failed regular expression match actually return? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.