> > > Does anyone know why m//g only returns true on success instead of
> > > returning the number of matches like s///g and tr/// do.
> >
> > ...I can't think of any reason ... It just seems like it should.
>
> ... I think it's more a matter of usage --
I wrote this earlier, when no one had given a decent reply, but
got waylaid. Now japhy has answered it, but I'm gonna post
anyway...
In general, with perl, don't expect consistency. This is just one
of many principles which are considered sacrosanct in many
other languages, but not in perl. Instead, think of something
half way between a computer language and a natural human
language like English, with all its odd twists and turns. More
on this in a moment.
So, why does m//g return true rather than the number of matches?
Well, if you want the number of matches, use m//g in list context.
What sort of answer is that? Well, if you want to know why a
given piece of syntax doesn't do something you expect, ask
yourself if there is another way to do that thing. If there is, then
there's a good chance that perl picks something *else* useful
to do with the syntax you're wondering about.
When you use m//g in scalar context, it returns true -- and
sets \G, moving the match forward in the match target string.
This can be very useful.
For more info, see:
perldoc -f pos
More on the consistency thing. As you become more experienced
in perl, you'll discover that perl is highly consistent, for some right
brained definition of consistent, at a higher level. (In the more
advanced lists, you will often see some particular piece of syntax
being called 'perlish'.)
If you stick with perl, this quickly starts to work in your favor. With
most languages, the curve tracking how many fundamental neat
language tricks you learn over time is oriented heavily toward
either beginners or advanced users, and seldom accomodates
both well. Perl has an unusual curve that manages to be ok (maybe
a little hard) for beginners but delights, and keeps on delighting for
years to come, those who keep on learning. It achieves this partly
by avoiding slavish adherence to any given design value, such as
"consistency".
An interesting related article:
http://www.perl.com/pub/2001/05/22/trafficlights.html