Jonathan Scott Duff wrote:
>
> How about something like this?
>
> $re = qr/(\d\d)-(\d\d)-(\d\d)/g;
> $re->onmatch_callback(push @list, makedate(^0,^1,^2));
> $string =~ $re;
>
It's not bad, but it loses one thing that I was trying to keep from the
SNOBOL model. If you have (again, improvised syntax - I *know* you want
to use the $ variables, OK? This is just for discussion):
/($pat1)($pat2)($pat3)(?{sub1(@\)$pat4|?{sub2(@\)}$pat5|?{sub3(@\)})/
This would translate to "if pat1pat2pat3 matches, call sub1 with all the
matches to that point if pat4 matches afterward, otherwise call sub2
with all the matches if pat5 matches, else just call sub3." The key bit
here is that you pass over the sub call, deferring it until you've
decided if the whole match worked, then picking the one that succeeded
and calling it. If you don't like the syntax, please feel free to
propose another. @\ seemed a good mnemonic for "the array of
backreferences I already matched".
And, of course, if you assume that @\ keeps growing when you use /g,
then doing a scalar @\and dividing by the number of backreferences would
give you a match count:
$string /(\d\d)-(\d\d)-(\d\d)/g;
$hits = scalar(@\)/3;
Of course, with multiple alternatives with different numbers of
backreferences leads to a problem, so maybe this is all academic. Oh well.
--- Joe M.