develooper Front page | perl.perl5.porters | Postings from July 2009

Re: [PATCH] Remove implicit split to @_

Thread Previous | Thread Next
John W. Krahn
July 10, 2009 04:08
Re: [PATCH] Remove implicit split to @_
Message ID:
Aristotle Pagaltzis wrote:
> * Michael G Schwern <> [2009-07-09 23:05]:
>> Yitzchak said:
>>> I don't get why you'd want to do that. If I wanted to count
>>> space-separated fields, I'd do:
>>> $count = 1 + $thing =~ y/ //;
>> Yitzchak's altnerative approach is very clever but underscores
>> the need for making split in scalar context just DWIM.
>> Because... damn, that's how you're supposed to count fields?
> That only works if the field separator is exactly one character,
> anyway. OTOH `1 + $str =~ m//g` works in the general case and is
> far less unintuitive.

`1 + $str =~ m//g` doesn't work at all.  The match operator is empty so 
it will use the regular expression from the last successfull match, 
which will probably not match just whitespace, or else it will match 
between every character.  But more importantly, because the match 
operator is used in an arithmetic expression it is in scalar context and 
so will return either true or false (1 or 0 in numerical context) and 
not the number of matches.

Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About