in reply to Loading all files in a dir with use via for loop

You are trying to load a module at runtime, so your solution will depend upon 'require', as 'use' is evaluated before the rest of your code.

So, in your first code segment, the 'use $plugin' is evaluated before $plugin contains any data, and is doomed to fail.

Your second code segment merely places some text (which happens to be 'use something_or_other') into a variable, then evaluates the variable in a void context (which in this case means that the value of the variable is returned and disappears, unused, immediately).

Try replacing the 'use' in the first segment with

eval "require $plugin";
- then as long as $plugin contains something that looks like a module name, you should load and evaluate the code in that module at run-time. The eval is required as you have generated the code to be executed on the fly, and perl has not yet had a chance to generate the necessary opcodes that it must run; when you give the string containing "require $plugin" to eval, it assumes the string contains Perl code, and compiles and runs it.

You say that you've tried 'eval' and 'require' but without seeing the code, it's hard to say what went wrong before.

Steve Collyer

Replies are listed 'Best First'.
Re^2: Loading all files in a dir with use via for loop
by scollyer (Sexton) on Sep 28, 2005 at 15:12 UTC
    Oops, the dangers of cut-n-paste and typing without thinking ...

    You don't need the eval, in fact. Ignore that, as require works fine on a variable. The code I pasted was taken from code that needs to catch an attempt to load a non-existent module (and should be wrapped in an if statement)

    If you know the modules are present, just use the bare 'require $plugin'.

    Steve Collyer