in reply to Loading a Module from a Variable

can you explain why your app requires loading from an array?

But i think what you're looking for is to eval the string, but note that this doesn't have the double-loading protection that require provides ...
if( eval join("", @mod) ){ print "module loaded\n"; } else { print "module failed to load: $@\n"; }
(note the change from $! to $@ -- see perlvar)

Replies are listed 'Best First'.
Re^2: Loading a Module from a Variable
by Anonymous Monk on Sep 21, 2005 at 13:32 UTC
    Thanks for the quick reply,

    My perl application takes as input a module and dynamically loads it, then calls the necessary functions. I could easily take the input argument (array) and write it to a temporary file, and require it, however I am using Perl...so there must be a way...

    I had tried your suggestion, however I ran into some problems since my module is defines something like:
    package testmod; use DynaLoader; use Exporter; BEGIN { @ISA = qw(Exporter, DynaLoader); @EXPORT = qw(testfunc); @EXPORT_OK = qw(); } use strict; sub testfunc { ... } 1;
    The errors I am getting are "Global symbol @ISA requires explicit package name..."

    PS: Thanks for the heads up on $@, I always get those confused.

    zi99er
      eval, unlike require does not evaluate the code in a new top-level lexical scope. Any lexically scoped varaibles or pragmas will be inherited by the code.

      One way to avoid use strict and use warnings being inherited by your evaled source is to simply no strict; no warnings; before you eval.

      To avoid lexical variables, put the eval() in a subroutine that appears before any file scoped lexicals (and before the pragmas too).

      # Start of program sub myeval { eval shift } use strict; use warnings; # ...rest of program

      A completely different approach would be to put a code reference into @INC. That way you can actually use require.