Search code examples
perlconstantsexporter

Can I export constants created with enum::fields in perl?


Can I export constants created with enum::fields? For example:

package XLine {
    use enum::fields qw{VAL SLOPE INTERVAL};
    use parent qw(Exporter);

    our @EXPORT = qw(VAL SLOPE INTERVAL);

    sub new {
    my $class = shift;
    my $self = bless [], $class;
    return($self);
    }
}

my $x = XLine->new();

printf("INTERVAL = %d\n", INTERVAL);

Is there a different "constant" package I should use instead? I used this one because I read that it was fast, here: http://neilb.org/reviews/constants.html


Solution

  • enum::fields is not the problem here. The code works when you add XLine->import(qw(INTERVAL)); or, more realistically/following best practices, move the XLine package into its own .pm file.