I am looking the most efficient and readable way to export all constants from my separate module,that is used only for storing constants.
For instance
use strict;
use warnings;
use Readonly;
Readonly our $MY_CONSTANT1 => 'constant1';
Readonly our $MY_CONSTANT2 => 'constant2';
....
Readonly our $MY_CONSTANT20 => 'constant20';
So I have a lot of variables, and to list them all inside our @EXPORT = qw( MY_CONSTANT1.... );
It will be painful. Is there any elegant way to export all constants, in my case Readonly variables(force export all ,without using @EXPORT_OK).
Actual constants:
use constant qw( );
use Exporter qw( import );
our @EXPORT_OK;
my %constants = (
MY_CONSTANT1 => 'constant1',
MY_CONSTANT2 => 'constant2',
...
);
push @EXPORT_OK, keys(%constants);
constant->import(\%constants);
Variables made read-only with Readonly:
use Exporter qw( import );
use Readonly qw( Readonly );
our @EXPORT_OK;
my %constants = (
MY_CONSTANT1 => 'constant1',
MY_CONSTANT2 => 'constant2',
#...
);
for my $name (keys(%constants)) {
push @EXPORT_OK, '$'.$name;
no strict 'refs';
no warnings 'once';
Readonly($$name, $constants{$name});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With