BigLug has asked for the wisdom of the Perl Monks concerning the following question:
I have a module that takes a list of country codes in its constructor. Each of these codes could (should) be another module that just contains a hash (%MyModule::AU::Hash, %MyModule::FR::Hash etc). I have several questions:
1. Is there a 'best' or simple way to load all the modules? I figure an evaled require in a loop. However I don't want to eval user input. Can anyone suggest a better way to do it?
2. Once they're loaded, how should I access them? At several points in my code I want to use the hash. One thought was to use eval to import the hash at load time, however I'm still sending user input to eval!:foreach my $cc (@country_code) { eval( "require MyModule::$cc" ); croak("Unable to locate MyModule::$cc") if $@; }
However I'd prefer not to import it all if I can. I'd prefer to store a reference to the hash in my object or even to grab the data as needed. But I don't want to fill my module with eval()s Thoughts and comments welcome.foreach my $cc (@country_code) { eval( "require MyModule::$cc" ); croak("Unable to locate MyModule::$cc") if $@; push( @{$self->{data}}, { eval( "\%MyModule::$cc::Hash" ) } ); }
Update: I can't validate the user input with known values. However I can validate it using a regex: croak if $cc !~ /^[a-z]{2}$/i
"Get real! This is a discussion group, not a helpdesk. You post something, we discuss its implications. If the discussion happens to answer a question you've asked, that's incidental." -- nobull@mail.com in clpm
Back to
Seekers of Perl Wisdom