开发者

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

开发者 https://www.devze.com 2023-03-22 11:26 出处:网络
=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..开发者_StackOverf
=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..开发者_StackOverflow社区.

Even this is not working:

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号