On #moose, the channel for a popular modern dialect of Object Oriented Perl, Debolaz asked:
<Debolaz> Is there a simple way to produce an array of array
like [[1..4],[1..4]] without having to specify the
[1..4] multiple times?
Matt Trout's reply confused me at first
<@mst> map { [ 1..4 ] } (1, 2)
because I thought there was a more straight-forward obvious answer:
([1..4]) x 2
Of course, as it happens, I was wrong… there is a problem with my
version, and it's clear if you print the structure out using Data::Dumper
$VAR1 = [
[ 1, 2, 3, 4 ],
$VAR1->[0]
];
The same reference is cloned. This wouldn't be a problem in Haskell
of course — being a pure language, there's no way reusing the same
memory address could cause a problem. So you can just write:
> replicate 2 [1..4]
Peregrin suggested
(eval { [1..4] }) x2
but this also reused the array. My best solution was
use Storable qw(dclone);
map { dclone $_ } ([1..4]) x 2
which is also quite yucky. Debolaz ended up using a variant of Matt's solution:
map [ 1..4 ], (1,2)
I never use map EXPR, LIST style (probably I read that it could be
confusing at an impressionable age…) but that does seem fairly
legible.