From 7ad6420a0344a66328855777d99dd5d78898b3c7 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Thu, 16 Nov 2017 16:55:39 +0000 Subject: Use named capture groups for thunks in match step. This allows the use of normal capture groups in templates without the use of the ?: modifier. --- xmpt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/xmpt b/xmpt index f23a3b2..b422e2d 100755 --- a/xmpt +++ b/xmpt @@ -194,6 +194,7 @@ sub do_send { sub do_recv { my $name = shift; my (@reparts, @thunks); + my $i = 0; foreach my $plan (@_) { # Text is matched explicitly, thunks are treated as wildcards, # with the match sent as an argument. @@ -201,8 +202,9 @@ sub do_recv { my $t = $plan->[1]->(); push @reparts, $t; } else { - push @reparts, "(.*)"; + push @reparts, "(?.*)"; push @thunks, $plan->[1]; + $i++; } } my $re = join '', @reparts; @@ -223,11 +225,11 @@ sub do_recv { die "SIGPIPE received from '@ARGV' while matching in $name step.\n"; }; alarm $timeout; - my @matches; + my %matches; while (1) { - @matches = ($readbuf =~ /$re/); - if (@matches >= @thunks) { + if ($readbuf =~ /$re/) { $readbuf = $'; + %matches = %+; last; } my $r; @@ -240,7 +242,7 @@ sub do_recv { alarm 0; for (my $i = 0; $i < @thunks; $i++) { - $thunks[$i]->($matches[$i]); + $thunks[$i]->($matches{"thunk$i"}); } } -- cgit v1.2.3