in reply to Optimize a pluggable regex matching mechanism

You can use the experimental (?{...}) code groups in the generated regex:

use strict; use warnings; use 5.010; use re 'eval'; my %h = ( '^foo' => sub { say 'foo' }, '^bar' => sub { say 'bar' }, ); my $what; my $re = join '|', map { "$_(?{\$what = '$_'})" } keys %h; if ('foo' =~ m/$re/o) { $h{$what}->(); }

But be sure to read the warnings in perlre.

Note that this requires the keys not to contain ' or \, but you could easily give them identifier labels instead, or properly escape $_ before interpolation.

(5.010 only required for say).

Perl 6 - links to (nearly) everything that is Perl 6.