mobrule, Axeman and Sinan answered your question: you created a data structure 3 layers deep, not 2.
I am surprised that no one suggested using the core Data :: Dumper library to help understand data structure issues.
use Data::Dumper; my @a = [[1]]; print Dumper \@a;
Fingerprints:
$ VAR1 = [[[1]]];
Your error used the array reference constructor [] instead of using parentheses (or nothing). @a = ([1]);
So why does Perl have weird syntax [] and {} for declaring hash links and array references?
It comes down to Perl aligning lists. This means that if you say: @a = (@b, @c); that @a assigned the contents of @b , concatenated by the contents of @c . This is the same as the Python extend list method. If you want @a have two elements, you need to make them not expand. You do this by taking a reference to each array: @a = (\@b, \@c); . This is similar to the Python append list method.
If you want to create anonymous nested structures, you need to specify them as hashes or arrays. What is where the syntax mentioned above comes in.
But what is the use of the list? Why is it worth using this unusual syntax to create arrays and hash links that is different from the syntax used for normal initialization?
Smoothing the list means that you can easily compile a list of subroutine parameters in a variable and then pass them without doing anything complicated: foo(@some_args, 5, @more_args); . See Apply on wikipedia for information on how this concept works in other languages. You can do all sorts of other nice things with map and other functions.
source share