从 Perl 模块导出所有常量(只读变量)的最有效方法是什么

What is the most efficient way to export all constants (Readonly variables) from Perl module

我正在寻找从我的单独模块中导出所有常量的最有效和可读的方法,该模块仅用于存储常量。
例如

use strict;
use warnings;

use Readonly;

Readonly our $MY_CONSTANT1         => 'constant1';
Readonly our $MY_CONSTANT2    => 'constant2'; 
....
Readonly our $MY_CONSTANT20    => 'constant20';

所以我有很多变量,并在我们的 @EXPORT = qw( MY_CONSTANT1.... );
中列出它们 这会很痛苦。有什么优雅的方法可以导出所有常量,在我的例子中是只读变量(强制导出所有,不使用@EXPORT_OK)。

实际常量:

use constant qw( );
use Exporter qw( import );    

our @EXPORT_OK;

my %constants = (
   MY_CONSTANT1 => 'constant1',
   MY_CONSTANT2 => 'constant2',
   ...
);

push @EXPORT_OK, keys(%constants);
constant->import(\%constants);

使用 Readonly 将变量设置为只读:

use Exporter qw( import );
use Readonly qw( Readonly );

our @EXPORT_OK;

my %constants = (
   MY_CONSTANT1 => 'constant1',
   MY_CONSTANT2 => 'constant2',
   #...
);

for my $name (keys(%constants)) {
   push @EXPORT_OK, '$'.$name;
   no strict 'refs';
   no warnings 'once';
   Readonly($$name, $constants{$name});
}

如果这些是可能需要插入到字符串等中的常量,请考虑将相关常量分组到哈希中,并使用 Const::Fast. This reduces namespace pollution, allows you to inspect all constants in a specific group etc. For example, consider the READYSTATE IE ReadyState [=] 的枚举值作为哈希常量 属性。无需为每个值创建单独的变量或单独的常量函数,您可以将它们分组在散列中:

package My::Enum;

use strict;
use warnings;

use Exporter qw( import );
our @EXPORT_OK = qw( %READYSTATE );

use Const::Fast;

const our %READYSTATE => (
    UNINITIALIZED => 0,
    LOADING => 1,
    LOADED => 2,
    INTERACTIVE => 3,
    COMPLETE => 4,
);

__PACKAGE__;
__END__

然后,您可以直观地使用它们,如下所示:

use strict;
use warnings;

use My::Enum qw( %READYSTATE );

for my $state (sort { $READYSTATE{$a} <=> $READYSTATE{$b} } keys %READYSTATE) {
    print "READYSTATE_$state is $READYSTATE{$state}\n";
}

另见 Neil Bowers' excellent review on 'CPAN modules for defining constants'

在回复@CROSP 时,您可以使用@ikegami 的 Readonly 方法,如下所示:

MyConstants.pm

package MyConstants;
<code from answer above>
1;

然后在foo.pl

use MyConstants qw($MY_CONSTANT1, $MY_CONSTANT2);
print "This is $MY_CONSTANT1\n";