clinton has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

What I would like to do is to load a language-specific regex on demand.

Is this a decent way of doing it?

package XYZ; my %Regexes; sub regex { my $class = shift; my $lang = shift || 'en'; unless (exists $Regexes{$lang}) { my $regex_class = "${class}::$lang"; eval qq{require $regex_class}; if ($@) { die "Error loading regex for lang '$lang' : $@"; } $Regexes{$lang} = $regex_class->regex() } return $Regexes{$lang} } package main; my $regex = XYZ->regex('en')

Replies are listed 'Best First'.
Re: Loading a language specific regex on demand
by Fletch (Bishop) on Apr 04, 2007 at 13:44 UTC

    You might could consider using AutoLoader to do this, or one of the plugin modules (just to toss out a couple more ideas).