You are trying to load a module at runtime, so your solution will depend upon 'require', as 'use' is evaluated before the rest of your code.

So, in your first code segment, the 'use $plugin' is evaluated before $plugin contains any data, and is doomed to fail.

Your second code segment merely places some text (which happens to be 'use something_or_other') into a variable, then evaluates the variable in a void context (which in this case means that the value of the variable is returned and disappears, unused, immediately).

Try replacing the 'use' in the first segment with

eval "require $plugin";
- then as long as $plugin contains something that looks like a module name, you should load and evaluate the code in that module at run-time. The eval is required as you have generated the code to be executed on the fly, and perl has not yet had a chance to generate the necessary opcodes that it must run; when you give the string containing "require $plugin" to eval, it assumes the string contains Perl code, and compiles and runs it.

You say that you've tried 'eval' and 'require' but without seeing the code, it's hard to say what went wrong before.

Steve Collyer


In reply to Re: Loading all files in a dir with use via for loop by scollyer
in thread Loading all files in a dir with use via for loop by Delusional

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.