Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: What does a failed regular expression match actually return? (updated)

by haukex (Archbishop)
on May 20, 2020 at 09:36 UTC ( [id://11116971]=note: print w/replies, xml ) Need Help??


in reply to What does a failed regular expression match actually return?

Usually where Perl operators return false they do so by either returning an empty string or 0

Actually, they return both at the same time.

What is the actual value returned by a failed regular expression match?

Please see this table of regular expression return values. print Dumper( "a" =~ m/b/ ); is providing list context to its arguments, so it's the same as writing print Dumper( );

Why does ... !~ returns the more common empty string version?

Because "a" !~ m/a/ is the same as !('a' =~ /a/), and that returns Perl's special false value.

Update: Your initial issue can be solved by scalar or PerlX::Maybe:

warn Dumper( { a => scalar( "a" =~ m/b/ ), b => 'asdf' } ); use PerlX::Maybe; warn Dumper( { maybe a => "a" =~ m/b/, b => 'asdf' } );
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).

Sorry, no, your analysis is not correct - the hash assignment is entirely in list context, in the examples you showed there's no scalar context going on there at all; only in the my $a = ... assignments. Update 2: To nitpick myself a little, the arguments to the =~ operator are in scalar context.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11116971]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-04-25 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found