good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Result of failed regexp match not pushed onto arrayby haukex (Archbishop) |
on Feb 16, 2019 at 12:26 UTC ( [id://1229990]=note: print w/replies, xml ) | Need Help?? |
This is about scalar vs. list context. A failed match (in this case, 'abc' =~ /abcd/) returns the empty list in list context, which is why push @foo, ('abc' =~ /abcd/);, which takes its arguments in list context, pushes nothing onto @foo. On the other hand $bar = ('abc' =~ /abcd/); causes evaluation of the match in scalar context (because the thing on the left-hand side of the assignment operator is a plain scalar), which means that $bar is assigned Perl's special false value, and then that gets pushed onto @foo. Update: If you want to always push a scalar onto the list, you could say push @foo, scalar('abc' =~ /abcd/);. Update 2019-08-17: Updated the link to "Truth and Falsehood".
In Section
Seekers of Perl Wisdom
|
|