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.
In reply to Re: What does a failed regular expression match actually return? (updated)
by haukex
in thread What does a failed regular expression match actually return?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |