in reply to Loading Modules At Run-Time With Interpolation

With your updates, I see the problem. You're using the package names without the Foo:: prefix. You're probably best off Foo-izing your submodules during the load and then from there on in, you don't need to worry about it:

package Foo; sub new { my $class = shift; my @submodules = map { "Foo::$_" } @_; for ( @submodules ) { eval "require $_" || die "$@"; } bless { submodules => \@submodules }, $class; } 1;
Hope that helps,

Replies are listed 'Best First'.
Re^2: Loading Modules At Run-Time With Interpolation
by Cody Pendant (Prior) on Aug 12, 2005 at 04:30 UTC
    I AM using them with the Foo prefix, in the section under "This doesn't work".


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re^2: Loading Modules At Run-Time With Interpolation
by Cody Pendant (Prior) on Aug 12, 2005 at 04:35 UTC
    ...but, having said that, your method works! Thanks!

    So my question is now -- how come "require String::$varable" doesn't work but if you do that map on the array to add the String:: part first, it does?



    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print

      You are misdiagnosing the problem. The problem isn't the require. It's where you try to use the details function. For example, both of these will work:

      my $p = 'Foo::Bar'; eval "require $p"; print $p->details->{name}; # ... and ... my $p = 'Bar'; eval "require Foo::$p"; print "Foo::$p"->details->{name};
      By merging Foo:: directly into your submodules list, we don't need to constantly recreate the full package name each time - it's already created.

        Oh dear. I feel so dumb now. Thank you. It's working perfectly now.


        ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
        =~y~b-v~a-z~s; print