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?

foreach my $cc (@country_code) { eval( "require MyModule::$cc" ); croak("Unable to locate MyModule::$cc") if $@; }
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 $@; push( @{$self->{data}}, { eval( "\%MyModule::$cc::Hash" ) } ); }
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.

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

In reply to Dynamically wrapping multiple modules by BigLug

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.