I'm not entirely clear on what you're doing or why. You point to an eval in a loop, and it sounds as if you want to take the separate parts of the eval and put them in different places.

You say you want to remove dynamically instantiating the module from the loop. I'm guessing you're talking about the use part of what you're doing. It looks as if inside the loop is where you know what modules you want to use, so that's where you'll need to use them. If you want, you can loop twice.

while ( ... ) { my $cat = $$methods{'category'}; my $class = 'Category::' . $category{$cat}->[0]; eval "use $class;" die $@ if $@; } while ( ... ) { my $cat = $$methods{'category'}; my $class = 'Category::' . $category{$cat}->[0]; my $method = $category{$cat}->[1]; eval "$class->new()->$method(\$key, \$params, \$testCase)"; die $@ if $@; }

Note, though, that in the second case, you don't really need the string interpolation. You could do this instead:

eval { $class->new()->$method( $key, $params, $testCase ) };

Perhaps you're trying to avoid doing the use part more than once for each module. This tends not to be a problem. If the module's already loaded, use does not load it again anyway.


In reply to Re: Dynamic use/require by kyle
in thread Dynamic use/require by Elijah

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.