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

perlperlperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I was wondering if there is a capability in Perl to register a call back to the regex engine and call that registered function each time a particular match is made. For example, consider the string: "this is a string". I want to count the number of 's' in the string. Normally I would use a string utility and pop one character at a time and count the 's' as I come across them. I think a better way would be to make a regex that has a callback to a character class, \s\ in this case, and increments a global variable each time it matches an 's'.