如何缩短常量名称?

时间:2014-01-29 20:09:22

标签: perl module constants perl-module

我有一个模块A::B::C::D::E。在模块中,我定义了一些常量:

use constant {  
    PERSON => 'person',  
    EMPLOYEE => 'employees',  
};

我做:

our @EXPORT_OK qw / PERSON EMPLOYEE /;

use另一个脚本中的模块,如果我这样做,常量会起作用:

A::B::C::D::E::PERSON

如何使用PERSON而不必包含完整的模块名称?我在我的脚本中导入PERSON但它不起作用。

2 个答案:

答案 0 :(得分:8)

@EXPORT_OK只标记“可用于导出”(假设您已正确地将模块连接到Exporter)。默认情况下不会导出它们。

在您的脚本中,执行

use A::B::C::D::E qw / PERSON EMPLOYEE /;

从模块中导入这些常量。

更新:听起来您还没有正确地将模块连接到Exporter。为此,您可以在A/B/C/D/E.pm中加入:

use Exporter 5.57 'import'; # v5.57 introduced an exportable import method

use Exporter ();
our @ISA = qw(Exporter); # also include any other base classes you have

我更喜欢第一种方法,它不会使您的包成为Exporter的子类。

答案 1 :(得分:2)

=之后您遗失our @EXPORT_OK

our @EXPORT_OK = qw( PERSON EMPLOYEE );
相关问题