I was recently wondering how to do this, and why a regular anonymous sub (w/o closures) wouldn't work. Then I found this node. Though my solution (before finding this) was to generate an anonymous sub using
eval $string. E.g.
my $range_checker = eval 'sub { local $_ = shift; /foo/../bar/ }';
At first, your way seems a little too indirect and magical, but it makes perfect sense if you create a 'flip-flop generator':
sub mk_flip_flop {
my ( $re1, $re2 ) = @_;
sub { local $_ = shift; /$re1/../$re2/ }
}