Perl 5 version 12.0 documentation
- delete EXPR
Given an expression that specifies an element or slice of a hash,
deletedeletes the specified elements from that hash so that exists() on that element no longer returns true. Setting a hash element to the undefined value does not remove its key, but deleting it does; see exists.
It returns the value or values deleted in list context, or the last such element in scalar context. The return list's length always matches that of the argument list: deleting non-existent elements returns the undefined value in their corresponding positions.
delete() may also be used on arrays and array slices, but its behavior is less straightforward. Although exists() will return false for deleted entries, deleting array elements never changes indices of existing values; use shift() or splice() for that. However, if all deleted elements fall at the end of an array, the array's size shrinks to the position of the highest element that still tests true for exists(), or to 0 if none do.
Be aware that calling delete on array values is deprecated and likely to be removed in a future version of Perl.
%ENVmodifies the environment. Deleting from a hash tied to a DBM file deletes the entry from the DBM file. Deleting from a
tiedhash or array may not necessarily return anything; it depends on the implementation of the
tiedpackage's DELETE method, which may do whatever it pleases.
delete local EXPRconstruct localizes the deletion to the current block at run time. Until the block exits, elements locally deleted temporarily no longer exist. See Localized deletion of elements of composite types in perlsub.
The following (inefficiently) deletes all the values of %HASH and @ARRAY:
And so do these:
But both are slower than assigning the empty list or undefining %HASH or @ARRAY, which is the customary way to empty out an aggregate:
The EXPR can be arbitrarily complicated provided its final operation is an element or slice of an aggregate: