http://www.perlmonks.org?node_id=45500


in reply to Getting the number of times a regexp matches

No one mentioned the canonical and most Perlish way to do this:

my $count = () = $str =~ m/pattern/g;

I suspect it offers little benefit in terms of efficiency, and can be confusing contextually. I do feel that it's the most succinct and elegant, however.

Update: My apologies; while I was writing this and getting distracted, it was mentioned ahead of me, and an interesting discussion of it followed. Ah well.

-dlc