in reply to Finding out which of a list of patterns matched
You could capture the match (read perldoc perlre on capturing brackets) and use the capture result to determine your program flow. One elegant way of doing this is to use a hash with the possible matches as hash keys, and the corresponding actions as the hash values in a subroutine reference (this is commonly called a "dispatch table"):
#!/usr/bin/perl use warnings; use strict; use Carp; my $input = shift or croak "Give me an argument, fool!\n"; my %action = ( foo => sub { print "fooing\n" }, bar => sub { print "barred\n" }, baz => sub { print "all bazzed out\n" }, ); if (my ($match) = $input =~ m/(foo|bar|baz)/ ){ &{$action{$match}}; } else { croak "Dunno what to do\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding out which of a list of patterns matched
by lemnisca (Sexton) on Jan 23, 2006 at 00:50 UTC | |
by BrowserUk (Patriarch) on Jan 23, 2006 at 01:45 UTC |