in reply to Problems counting regex matches

Just a stylistic note in addition to those of Eily above: It is uselessly verbose to test for a match against a regex and then conditionally count the number of matches by doing another match against the exact same regex (or else setting the count to zero) as in the OPed code. Just counting valid matches (i.e., somehow avoiding those extraneous captures) is enough: if there is no match at all, the count will be 0.

>perl -wMstrict -le "use 5.010; ;; my $regex = qr{ (?| (AAA) | (BBB) | (XXX) | (YYY) ) }xms; ;; my $s = 'AAA BBB CCC DDD AAA BBB'; my $n_matches =()= $s =~ m{ $regex }xmsg; print qq{$n_matches matches}; ;; $s = 'foo bar baz boff'; $n_matches =()= $s =~ m{ $regex }xmsg; print qq{$n_matches matches}; " 4 matches 0 matches