in reply to Result of failed regexp match not pushed onto array
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".
|
|---|