This seems to be exactly the solution I'm after, however I wonder about the consequences in a mod_perl context...

Say I have a module called DO_STUFF.pm, and DO_STUFF has the following code in it:
package DO_STUFF.pm; BEGIN { use strict; use Exporter; use vars qw(@ISA @EXPORT); my $conf = determine_conf_module_name(); eval "use $conf;"; } do_stuff....etc...
Now I load DO_STUFF.pm into memory for mod_perl and at loading time I would assume that $conf is determined and that module is now loaded into memory space for use by DO_STUFF.

But what happens when I am using DO_STUFF in a context that changes what determine_conf_module_name() returns, i.e. a new $conf, how would DO_STUFF.pm (already loaded into memory) know to use a different module?

Would it be better to execute
my $conf = determine_conf_module_name(); eval "use $conf;";
during instantiation, like this:
package DO_STUFF.pm; use strict; use Exporter; use vars qw(@ISA @EXPORT); ... sub new { my $conf = determine_conf_module_name(); eval "use $conf;"; ... }
Or is the BEGIN{} block run only during instantiation of the object so I'm fine...?

oooOOOOooo....my head is all twisty now...

THANKS ALL!!!

Tosh

In reply to This works, but at what cost...? by tosh
in thread Are dynamic 'use' statements possible? by tosh

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.