There is a restriction on arrays and hashes as state variables. We can’t initialize them in list context as of Perl 5.10:
So
state @array = qw(a b c); #Error!
Why is it so? Why this is not allowed?
We can use state arrays and initialize them by this way
state @numbers;
push @numbers, 5;
push @numbers, 6;
but why not directly do it by state @numbers = qw(5 6);
Why doesn’t Perl allow it?
According to perldiag, support for list context initialization is planned for a future release:
According to this message about the change that made this an error:
You could always use an arrayref instead:
Note that your example of
does not mean the same thing that
state @numbers = qw(5 6)would (if it worked). Astatevariable is only initialized once, but your code would push 5 & 6 onto the array every time that code was executed.