For example, $+{foo} is equivalent to $1 after the following match: 'foo' =~ /(?foo)/; #### sub { /\G (?: ... (?{ code1(...); }) # pattern 1, handler for pattern 1 | ... (?{ code2(...); }) # pattern 2, handler for pattern 2 | ... (?{ code3(...); }) # and so on )/gcx; } #### sub parse { my $input= shift; my $code= ... # assemble regex sub text like above my $lexer= eval $code or die "BUG: syntax error in generated code: $@"; local $_= $input; &$lexer || die "Syntax error at '" . substr($_, pos, 10) . "'" while pos < length; }