eval和use的问题



我编写了这段代码,它在系统中安装POE模块时可以工作。

#!/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.

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

问题是eval在编译后运行,但是您的KERNELHEAP常量在编译时检查。所以你需要把eval放到BEGIN代码块中:

BEGIN {
    eval "use POE;";
    die "Unable to load POE: $@n" if $@;
}

虽然这主要是徒劳的练习,因为一个标准的use POE;也会死与一个有用的错误,如果它不能加载你所请求的模块

相关内容

最新更新