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

You don't say one way or the other, but I'm assuming you did not have warnings enabled in your code. Had you done so, Perl would have given you another clue as to what was going on.

c:\@Work\Perl\monks>perl -Mstrict -le "use warnings; use Data::Dumper; warn Dumper( { a => 'a' =~ m/b/, b => 'asdf' } ); " Odd number of elements in anonymous hash at -e line 1. $VAR1 = { 'a' => 'b', 'asdf' => undef };
The "Odd number of elements in anonymous hash ..." warning can be explained as follows:
As haukex has explained, the expression
    { a => 'a' =~ m/b/,  b => 'asdf' }
evaluates to
    { 'a', (empty list), 'b', 'asdf' }
which flattens to
    { 'a', 'b', 'asdf' }
which gives you the perplexing hash structure.

Bottom line: always use strict and warnings (if you weren't doing so already), even when you know you don't really need them! :)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: What does a failed regular expression match actually return?
by ikegami (Patriarch) on May 20, 2020 at 20:35 UTC

    Nit: Returning an empty list simply means "putting no scalars on the stack". It doesn't return something that gets flattened.