导出后变量仍然不可见

Exported variable is still not visible after exporting

我正在开发一个简单的 Perl 模块来创建和验证音符并找到等音等价的音符。我在模块中存储一个包含所有有效注释的数组引用,然后将其导出,以便 Note.pm 模块可以查看哪些注释有效,并在创建 Note 对象时对照列表进行检查。

问题是,无论我尝试什么,导出的 $VALID_NOTES 数组引用在 Note.pm 中都不可见!我已经阅读了 Exporter 上的文档大约一千遍,并回顾了我使用 Exporter 的大量旧 Perl 模块,但我无法弄清楚这里出了什么问题...

代码如下:

test.pl

use strict;
use warnings;
use Music;

my $m = Music->new();

my $note = $m->note('C');

print $note;

Music.pm

package Music;

use Moose;
use Note;

use Exporter qw(import);
our @EXPORT_OK = qw($VALID_NOTES);

no warnings 'qw';

# Valid notes
# Enharmonic notes are in preferred (most common) order:
#     Natural -> Sharp -> Flat -> Double Sharp -> Double Flat
our $VALID_NOTES = [
    [ qw(C B#        Dbb) ],
    [ qw(  C# Db B##    ) ],
    [ qw(D       C## Ebb) ],
    [ qw(  D# Eb     Fbb) ],
    [ qw(E    Fb D##    ) ],
    [ qw(F E#        Gbb) ],
    [ qw(  F# Gb E##    ) ],
    [ qw(G       F## Abb) ],
    [ qw(  G# Ab        ) ],
    [ qw(A       G## Bbb) ],
    [ qw(  A# Bb     Cbb) ],
    [ qw(B    Cb A##    ) ],
];

sub note {
    my $self = shift;
    my $name = shift;
    return Note->new(name => $name);
}

__PACKAGE__->meta->make_immutable;

Note.pm

package Note;

use Moose;
use Music qw($VALID_NOTES);
use experimental 'smartmatch';

has 'name'  => (is => 'ro', isa => 'Str', required => 1);
has 'index' => (is => 'ro', isa => 'Int', lazy => 1, builder => '_get_index');

# Overload stringification
use overload fallback => 1, '""' => sub { shift->name() };

sub BUILD {
    my $self = shift;
    if (!grep { $self ~~ @{$VALID_NOTES->[$_]} } 0..$#{$VALID_NOTES}) {
        die "Invalid note: '$self'\n";
    }
}

sub _get_index {
    my $self = shift;
    my ($index) = grep { $self ~~ @{$VALID_NOTES->[$_]} } 0..$#{$VALID_NOTES};
    return $index;
}

sub enharmonic_notes {
    my $self = shift;
    my $index = $self->index();
    return map { Note->new($_) } @{$VALID_NOTES->[$index]};
}

__PACKAGE__->meta->make_immutable;

当我 运行 代码时,我得到这个输出:

Global symbol "$VALID_NOTES" requires explicit package name at Note.pm line 15.
Global symbol "$VALID_NOTES" requires explicit package name at Note.pm line 15.
Global symbol "$VALID_NOTES" requires explicit package name at Note.pm line 22.
Global symbol "$VALID_NOTES" requires explicit package name at Note.pm line 22.
Global symbol "$VALID_NOTES" requires explicit package name at Note.pm line 29.

Music.pm 中,在 加载 BEGIN 块 @EXPORT_OK 之前 加载 Note:

package Music;
use Moose;
our @EXPORT_OK;
BEGIN { @EXPORT_OK = qw($VALID_NOTES) }
use Exporter qw(import);
use Note;