Perl 导出变量在导出后仍然不可见
我正在开发一个简单的Perl模块,用于创建和验证音符,并找到与之和谐的音符。我正在存储一个包含模块中所有有效注释的数组引用,然后将其导出,以便Perl 导出变量在导出后仍然不可见,perl,scope,packages,exporter,Perl,Scope,Packages,Exporter,我正在开发一个简单的Perl模块,用于创建和验证音符,并找到与之和谐的音符。我正在存储一个包含模块中所有有效注释的数组引用,然后将其导出,以便Note.pm模块可以查看哪些注释有效,并在创建Note对象时对照列表进行检查 问题是,无论我尝试什么,导出的$VALID\u NOTES数组引用在Note.pm中都不可见!我已经阅读了上千次文档,回顾了大量使用Exporter的旧Perl模块,但我无法找出这里的错误 代码如下: test.pl use strict; use warnings; use
Note.pm
模块可以查看哪些注释有效,并在创建Note
对象时对照列表进行检查
问题是,无论我尝试什么,导出的$VALID\u NOTES
数组引用在Note.pm
中都不可见!我已经阅读了上千次文档,回顾了大量使用Exporter
的旧Perl模块,但我无法找出这里的错误
代码如下:
test.pl
use strict;
use warnings;
use Music;
my $m = Music->new();
my $note = $m->note('C');
print $note;
音乐.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;
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;
注意.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;
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
中,在加载注释之前,在开始块中填充@EXPORT\u OK
:
package Music;
use Moose;
our @EXPORT_OK;
BEGIN { @EXPORT_OK = qw($VALID_NOTES) }
use Exporter qw(import);
use Note;
回答。就个人而言,我会将$VALID_NOTES
移动到它所属的便笺中。您始终可以将其导入音乐并从音乐中重新导出。请不要使用smartmatch实验功能;它将从Perl中删除或以向后不兼容的方式进行更改。混合Moose和Exporter?我不希望如此。@ikegami:对于OO模块,我更希望使用包方法返回结构。@ikegami:好吧,最好将它伪装成变量:-)我得到C
作为输出。很有趣。。。我只是复制并粘贴了它,以确保我没有犯错误,而且它对我来说仍然失败。在其他地方有一个打字错误!它起作用了。谢谢我想我可以把这个变量移到“Notes”模块中,以完全消除这个问题。