Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: m//g in list and scalar context differences?

by Anno (Deacon)
on Sep 19, 2007 at 11:11 UTC ( [id://639864]=note: print w/replies, xml ) Need Help??


in reply to m//g in list and scalar context differences?

map { my $t = $2 || $3; print "<$t\n"; } m/('(.*?)'|([^']*?))($|,\s*)/g;
In list context, a global match that contains capturing parentheses returns a flattened list of all captured parts (three per match in your example) for all matches. That is the list you're mapping over, and it is built before the map even starts. The capture variables ($1, $2) you are refering to in the map block are the ones left over from the last of possibly many global matches.

In other words, map will run over a list three times as long as the number of global matches, but $2 and $3 will be the same during all iterations. That won't do at all what you expect.

Anno

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found