2011-11-29 45 views
2

我编写了这段代码,它在系统中安装了POE模块时起作用。eval和使用问题

#!/usr/bin/perl 

use strict; 
use warnings; 
use POE; 

... 

但我想,以确定是否该模块有:

#!/usr/bin/perl 

use strict; 
use warnings; 
eval("use POE; 1") or die ('Please, install POE module. \n'); 

... 

,并返回:

Bareword "KERNEL" not allowed while "strict subs" in use at ./terminalhero.perl line 58. 
Bareword "HEAP" not allowed while "strict subs" in use at ./terminalhero.perl line 60. 
Execution of ./terminalhero.perl aborted due to compilation errors. 

我尝试过其他的模块,也有错误。我如何使用严格模式来做我想要的?

+1

'terminalhero.perl'的内容是什么?特别是第58行和第60行。 – Trott

+0

请重新发布代码。如果参考文件不存在,SO就会变得无用 – JGurtz

回答

8

问题是eval在编译时间后运行,但是你的KERNELHEAP常量在编译时被检查。所以,你需要把你的eval内一个BEGIN块:

BEGIN { 
    eval "use POE;"; 
    die "Unable to load POE: [email protected]\n" if [email protected]; 
} 

虽然这多半是徒劳的,因为一个标准的use POE;也将与一个有用的错误死去,如果它不能加载模块你”已经要求。

+0

这样,如果程序感觉非常主动,它可以自动下载并要求安装缺失的模块。但是,真的,这可能会造成更多的伤害而不是好的。 –

+0

@JonPurdy:是的,如果你在做死亡之外的事情,那可能是有道理的。 – Flimzy

+0

我不会称有用的标准错误消息。与[由perl5i提供](http://p3rl.org/perl5i#Better-load-errors)相比较。 – daxim