Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Result of failed regexp match not pushed onto array

by haukex (Archbishop)
on Feb 16, 2019 at 12:26 UTC ( [id://1229990]=note: print w/replies, xml ) Need Help??


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".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found