http://www.perlmonks.org?node_id=582141


in reply to Please explain 'list' assignment.

Because list assignments are parallel. A list of three items on the left expects three items on the right. Since there's only one, $var2 and $var3 remain undefined.

You can do what you want with:

my ($var1, $var2, $var3) = "Hello" x 3;

Update: Oops - I read your question slightly wrong. To assign the same value three times, use

my ($var1, $var2, $var3) = map "Hello", 1..3;
or
my ($var1, $var2, $var3) = ("Hello") x 3;
(thanks Sidhekin)

Replies are listed 'Best First'.
Re^2: Please explain 'list' assignment.
by shenme (Priest) on Nov 03, 2006 at 19:19 UTC
    The 'x' operator can act upon strings or upon lists. The use above, "Hello" x 3" created three concatenated copies of the string "Hello", thus "HelloHelloHello". What you wanted was "three copies of an item in list context", like so:
    my ( $var1, $var2, $var3 ) = ("Hello") x 3;
    The parentheses force the value into list context, the 'x' then makes three copies of that single value list, and you get ("Hello", "Hello", "Hello"), which is what OP wanted.
Re^2: Please explain 'list' assignment.
by Bagarre (Sexton) on Nov 03, 2006 at 19:20 UTC
    Just came back to say that didnt work :) The map works... and makes sense.
    Entities should not be multiplied unnecessarily.
Re^2: Please explain 'list' assignment.
by Bagarre (Sexton) on Nov 03, 2006 at 19:12 UTC
    Perfect. Thank you :)
    Entities should not be multiplied unnecessarily.