我有一个Perl程序,需要使用包(我也写).其中一些包仅在运行系统中选择(基于某些环境变量).我不想在我的代码中为所有这些包放置一个“使用”行,当然,只有一个“使用”行,基于这个变量,如下所示:
奥伦
@H_301_4@不幸的是,这当然不行.有什么想法如何做到这一点? @H_301_4@提前致谢,
- use $ENV{a};
奥伦
解决方法
@H_301_4@“use”在这里不起作用,因为它只在eval的上下文中导入. @H_301_4@正如@Manni所说,其实最好是使用require.引用人perlfunc:
- eval "require $ENV{a}";
- 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";