On 26 Apr 2007, at 14:15, Andy Armstrong wrote:
    my %despatch = (
        'yahoo'  => sub { print "Yahoo!\n"; },
        'google' => sub { print "Google!\n"; },
    );

    my $match = join('|', map {quotemeta} keys %despatch);
    my $re = qr/($match)/i;       # or whatever

    print "Using: $re\n";
    for my $t ('I like yahoo', 'and also google') {
        $despatch{lc($1)}->() if $t =~ $re;
    }

Sorry; I meant to add some commentary to that.

If you're despatching on a number of words make a table that maps those words to the required actions (%despatch in the example), build an RE from the keys of that hash ($match, $re) and use that to find matching text /and/ extract the key to despatch on.

Then when you add new cases you only have to add them in one place and everything else just works.

--
Andy Armstrong, hexten.net

Reply via email to