DavidSzczesniak
12/11/2017 - 4:09 PM

Array slices

This allows you to access elements of an array in list context. It takes a list of zero or more indices and uses the array sigil (@).

my @youngest_cats = @cats[-1, -2];
my @oldest_cats   = @cats[0 .. 2];
my @selected_cats = @cats[@indexes];

# useful for assignment:
@users[@replace_indices] = @replace_users;

# note: an array slice always imposes list contex

# single-element array slice; *list* context 
@cats[-1] = get_more_cats(); 

# single-element array slice; *scalar* context
$cats[-1] = get_more_cats();