There isn't much of a penalty on eval STRING when it's a simple statement like use. Not compared to the time it takes to load the module! :)

You can always use require, but you first have to perform a transform on the module name (this is a design bug which is tidied in Perl 6):

my $module = "Some::Class"; $module =~ s{::}{/}g; $module .= ".pm"; require $module;

There will be a cargo cult disapproving of require, be sure to poke your tongue out at them, laugh and ask them why until they say "'coz" :-).

You can also look at Class::Tangram::Generator for an example of code that defers loading of modules until objects of the relevant type are created, using several different mechanisms.

$h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

In reply to Re: Object loading at runtime by mugwumpjism
in thread Object loading at runtime by benrwebb

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.