I’ve been using the [constant] pragma, and have a quick question on how I can declare a constant list:
use constant {
LIST_ONE => qw(this that the other), #BAD
LIST_TWO => ("that", "this", "these", "some of them"), #BAR
LIST_THREE => ["these", "those", "and thems"], #WORKS
};
The problem with the last one is that it creates a reference to a list:
use constant {
LIST_THREE => ["these", "those", "and thems"],
};
# Way 1: A bit wordy and confusing
my $arrayRef = LIST_THREE;
my @array = @{$arrayRef};
foreach my $item (@array) {
say qq(Item = "$item");
}
# Way 2: Just plain ugly
foreach my $item (@{&LIST_THREE}) {
say qq(Item = "$item");
}
This works, but it’s on the ugly side.
Is there a better way of creating a constant list?
I realize that constants are really just a cheap way of creating a subroutine which returns the value of the constant. But, subroutines can also return a list too.
What is the best way to declare a constant list?
According to the documentation, if you do:
…you can then do:
I’d say that’s nicer than the two ways of referencing constant lists that you have described.