Hi fellow monks,

Right now, i use eval() to dynamic load a module. eg:
request: script.pl?option=showOverview&....
in the script i do the following:
my $op = $query->param('option') || 'Login'; if ($op !~ /^[a-z0-9_]*$/i) { $op="NotAllowed"; } eval "require $op"; die "Couldn't find Class $op : ".$@."\n" if $@; $op->perform(...);

Now i use a regexp to avoid creative ones from using backticks and other system expressions in their request, however i feel not really secure....

I already thought of a way to create a hash with module names, so the $op is matched as a key value of it's module name, like this:
my $mods = {showOverview=>"showOverview", Login => "Login"} my $op = $mods->{$query->param('option')}; eval "require $op"; ...

Each time a module is generated, i have to change the script.pl as well however, and i don't like that in 'live' surroundings, (restart http etc).

I hope i'm clear in what i try to accomplish, and hope there are better wayst to accomplish this. Thoughts, ideas and examples are more than welcome.
Thanks in advance (again)

"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

In reply to better ways than eval to dynamic load a module by jbrugger

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.