Search code examples
perlscopepackageexporter

Exported variable is still not visible after exporting


I am working on a simple Perl module to create and validate musical notes and to find enharmonically equivalent notes. I am storing an array reference containing all valid notes in a module and then exporting it so the Note.pm module can see which notes are valid and check against the list when it goes to create a Note object.

The problem is, no matter what I try the Exported $VALID_NOTES array reference is not visible in Note.pm! I've read through the documentation on Exporter about a thousand times and looked back at tons of my old Perl modules that use Exporter and I just cannot figure out what is wrong here...

Here is the code:

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;

When I run the code I get this output:

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.

Solution

  • In Music.pm, populate the @EXPORT_OK in a BEGIN block before loading Note:

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