使用 %EXPORT_TAGS 的 Perl 模块

Perl Module using %EXPORT_TAGS

我无法在我的 Perl 模块中正确使用 %EXPORT_TAGS。在 Solver.pl 我有:

use MatrixFunctions qw(:Normal);

然后在MatrixFunctions.pm里面,我有:

package MatrixFunctions;

use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = ();
@EXPORT_OK   = qw(&det &identityMatrix &matrixAdd 
    &matrixScalarMultiply &matrixMultiplication);
%EXPORT_TAGS = ( Det => [qw(&det)],
    Normal => [qw(&det &identityMatrix &matrixAdd 
        &matrixScalarMultiply &matrixMultiplication)]);

但是只有当我有 @EXPORT_OK 包括所有方法时它才有效。如果我有

@EXPORT_OK   = ();

我有错误:

"matrixScalarMultiply" is not exported by the MatrixFunctions module
 "det" is not exported by the MatrixFunctions module
 "matrixAdd" is not exported by the MatrixFunctions module
 "matrixMultiplication" is not exported by the MatrixFunctions module
 "identityMatrix" is not exported by the MatrixFunctions module
Can't continue after import errors at Solver.pl line 6.
BEGIN failed--compilation aborted at Solver.pl line 6.

我认为在我的 Solver.pl 文件中使用 qw(:Normal) 的目的是让 @EXPORT_OK 为空。我做错了什么?

perldoc -f Exporter 在“高级功能”部分下:

e.g., Module.pm defines:

@EXPORT = qw(A1 A2 A3 A4 A5);
@EXPORT_OK = qw(B1 B2 B3 B4 B5);
%EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);

Note that you cannot use tags in @EXPORT or @EXPORT_OK.

Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.

上面的粗体部分说明您需要@EXPORT_OK或[=15=中拥有您希望在%EXPORT_TAGS中放置的功能]

我开始使用的模式是在 @EXPORT_OK 中定义我想要允许导出的所有内容,然后使用 @EXPORT_OK 构建一个 `:all' 标签:

our @ISA = qw(Exporter);
our @EXPORT_OK = qw/raspberry apple/;

our %EXPORT_TAGS = (
    'all' => \@EXPORT_OK,
);

[不是回答,而是跟进大的评论]

如果你想 @EXPORT_OK 自动填充,你可以使用以下方法:

push @EXPORTER_OK, map @$_, values %EXPORT_TAGS;

Exporter 不关心重复条目。如果这样做,您可以改用以下内容:

my %EXPORT_OK;
@EXPORT_OK = grep !$EXPORT_OK{$_}++,
    @EXPORT_OK, map @$_, values %EXPORT_TAGS;

因此,经过一些清理后,您的代码将如下所示:

package MatrixFunctions;

use strict;
use warnings;

use Exporter qw( import );

our $VERSION     = 1.00;
our @EXPORT      = ();
our @EXPORT_OK   = ();
our %EXPORT_TAGS = (
    Det    => [qw( det )],
    Normal => [qw( det identityMatrix matrixAdd matrixScalarMultiply matrixMultiplication )],
);

push @EXPORTER_OK, map @$_, values %EXPORT_TAGS;