asker
asker

Reputation: 2199

What's the difference b/w the 2 ways to load a module dynamically?

=any
            eval qq(
                use $$category_r[0];
                );
            die $? if $?;
=cut
            require "$$category_r[0].pm";

Now only require is working for me,I don't know why the 1st one doesn't work as expected...

Even this is not working:

my $pkg = "A";
eval {
    use $pkg;
    };

Upvotes: 0

Views: 101

Answers (2)

Dallaylaen
Dallaylaen

Reputation: 5308

Try Module::Load if you want to load modules on the fly.

It works for both filenames and modules. It is safer than

my $module = "strict; warn 'PWNED'";
eval "use $module"; 

Also, as @daxim points out, it explains what's going on.

Upvotes: 5

ysth
ysth

Reputation: 98378

You should be checking $@, not $?. Is this the actual code that doesn't work? Presumably you don't have it enclosed in pod for real.

For use, $$category_r[0] should be something like Module::Name, while your require is expecting Module/Name.

Upvotes: 2

Related Questions