Has anyone ever done this before when the name of the module is not known until runtime? Here is what I'm dealing with: At some point in the flow of my code, I pull a value from a database and store it in, say, $module. The value in $module is the fully-qualified package name of the module. Now, all modules pulled from the database adhere to a strict API, and must export the run() method, so I at least know that. However, I'm having trouble loading the module at all:
if ($module) {
   eval "use $module";
   eval "$module::run()";
}
I end up getting "Use of uninitialized value in concatenation (.)" on stderr, and it points to the line where I perform the `use'. Why? Shouldn't the EXPR version of `eval' be able to interpolate $module and hand it off to `use'? I even put parentheses around $module so that it might evaluate first, but to no avail. Now, hardcoding it with the name of the module that is getting assigned to $module works perfectly, so I know that the module is loadable (its path is in @INC and everything, and I can call it like foo::bar::run() just fine if I hardcode it). So, what's the story? Is it even possible to load a module during runtime when the module name is stored in a scalar? Michael

In reply to Loading a module at runtime... by Anonymous Monk

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.