Perl   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用仅在运行时才知道的Perl软件包?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Perl程序,需要使用包(我也写).其中一些包仅在运行系统中选择(基于某些环境变量).我不想在我的代码中为所有这些包放置一个“使用”行,当然,只有一个“使用”行,基于这个变量,如下所示:
use $ENV{a};

不幸的是,这当然不行.有什么想法如何做到这一点?

提前致谢,
奥伦

解决方法

eval "require $ENV{a}";

“use”在这里不起作用,因为它只在eval的上下文中导入.

正如@Manni所说,其实最好是使用require.引用人perlfunc:

If EXPR is a bareword,the require assumes a ".pm" extension and 
replaces "::" with "/" in the filename for you,to make it easy to 
load standard modules.  This form of  loading of modules does not 
risk altering your namespace.

In other words,if you try this:

        require Foo::Bar;    # a splendid bareword

The require function will actually look for the "Foo/Bar.pm" file 
in the directories specified in the @INC array.

But if you try this:

        $class = 'Foo::Bar';
        require $class;      # $class is not a bareword
    #or
        require "Foo::Bar";  # not a bareword because of the ""

The require function will look for the "Foo::Bar" file in the @INC 
array and will complain about not finding "Foo::Bar" there.  In this 
case you can do:

        eval "require $class";

大佬总结

以上是大佬教程为你收集整理的如何使用仅在运行时才知道的Perl软件包?全部内容,希望文章能够帮你解决如何使用仅在运行时才知道的Perl软件包?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: