Permanent Perl Array

use constant { COLUMNS => qw/ TEST1 TEST2 TEST3 /, } 

Is it possible to store an array using a constant package in Perl?

Whenever I try to use an array like my @attr = (COLUMNS); , it contains no values.

+4
source share
2 answers
 use constant { COLUMNS => [qw/ TEST1 TEST2 TEST3 /], }; print @{+COLUMNS}; 
+7
source

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 
+11
source

Source: https://habr.com/ru/post/1496594/


All Articles