- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

8.3.5 Localized deletion of elements of composite types

You can use the delete local $array[$idx] and delete local $hash{key} constructs to delete a composite type entry for the current block and restore it when it ends. They return the array/hash value before the localization, which means that they are respectively equivalent to

do {
    my $val = $array[$idx];
    local  $array[$idx];
    delete $array[$idx];
    $val
}

and

do {
    my $val = $hash{key};
    local  $hash{key};
    delete $hash{key};
    $val
}

except that for those the local is scoped to the do block. Slices are also accepted.

my %hash = (
 a => [ 7, 8, 9 ],
 b => 1,
)
{
 my $a = delete local $hash{a};
 # $a is [ 7, 8, 9 ]
 # %hash is (b => 1)
 {
  my @nums = delete local @$a[0, 2]
  # @nums is (7, 9)
  # $a is [ undef, 8 ]
  $a[0] = 999; # will be erased when the scope ends
 }
 # $a is back to [ 7, 8, 9 ]
}
# %hash is back to its original state
ISBN 9781906966027Perl Language Reference ManualSee the print edition