Array Assignment


You can also mix the variables on the right side and if there are arrays on the right side the whole thing becomes one flat array !


examples/perlarrays/assignment.pl
use strict;
use warnings;

my @x = ('foo', 'bar');
my @y = ('moose', 'barney');
my @z = (@x, @y);             # ('foo', 'bar', 'moose', 'barney');
print "@z\n";                 # foo bar moose barney

my $owner = 'Moose';
my @tenants = qw(Foo Bar);
my @people = ($owner, 'Baz', @tenants);  # ('Moose', 'Baz', 'Foo', 'Bar')
print "@people\n";                       # Moose Baz Foo Bar

my ($x, @y, @z);
($x, @y)     = f(); # where f() returns (1, 2, 3, 4);
                    # $x is 1;  @y is (2, 3, 4)
($x, @y, @z) = f(); # where f() returns (1, 2, 3, 4);
                    # $x is 1;  @y is (2, 3, 4)  @z is empty: ()

@z = ();            # Emptying an array