http://qs321.pair.com?node_id=101996


in reply to Returning _which_ pattern matched...?

If you happen to have a lot of patterns to match, I like the "build a sub on the fly" approach

my @patterns = qw/ foo bar baz /; ## if you need to return the pattern itself, return '$_' instead my $match_n_return = join( "\n", map{ "return \$1 if \$_[0] =~ /$_/o;" } @patterns ); my $sub = eval qq|sub { $match_n_return return undef }|; if( $@ ) { ## some syntax error in the eval string die $@ } my $matched = $sub->( "string you want to match" );

The only problem here is, you don't get a fine granularity of control for each match. But I like the fact that this can be used over and over, and that since all the regexes are pre-compiled, you save some time for repeated use