use constant {
COLUMNS => qw/ TEST1 TEST2 TEST3 /,
}
Can I store an array using the constant package in Perl?
Whenever I go on to try to use the array like my @attr = (COLUMNS);, it does not contain the values.
Or remove the curly braces as the docs show :-
1 use strict;
2 use constant COLUMNS => qw/ TEST1 TEST2 TEST3 /;
3
4 my @attr = (COLUMNS);
5 print @attr;
which gives :-
% perl test.pl
TEST1TEST2TEST3
Your code actually defines two constants COLUMNS and TEST2 :-
use strict;
use constant { COLUMNS => qw/ TEST1 TEST2 TEST3 /, };
my @attr = (COLUMNS);
print @attr;
print TEST2
and gives :-
% perl test.pl
TEST1TEST3
Use a + to signify that it's a contant.
use constant {
COLUMNS => [qw/ TEST1 TEST2 TEST3 /],
};
print @{+COLUMNS};
The + is a hint to the interpreter that the constant is actually a constant, and not a bare word. See this reply for more details.
perl -e 'use constant C => [1,2,3]; C->[1] = 5; print C->[1]. "\n";'