如何使用仅在运行时才知道的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";

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...