keys

Returns a list consisting of all the keys of the named hash, or the indices
of an array. (In scalar context, returns the number of keys or indices.)

The keys of a hash are returned in an apparently random order. The actual
random order is subject to change in future versions of Perl, but it
is guaranteed to be the same order as either the values or each
function produces (given that the hash has not been modified). Since
Perl 5.8.1 the ordering can be different even between different runs of
Perl for security reasons (see Algorithmic Complexity Attacks in perlsec).

As a side effect, calling keys() resets the internal interator of the HASH or ARRAY
(see each). In particular, calling keys() in void context resets
the iterator with no other overhead.

Used as an lvalue, keys allows you to increase the number of hash buckets
allocated for the given hash. This can gain you a measure of efficiency if
you know the hash is going to get big. (This is similar to pre-extending
an array by assigning a larger number to $#array.) If you say

then %hash
will have at least 200 buckets allocated for it--256 of them,
in fact, since it rounds up to the next power of two. These
buckets will be retained even if you do %hash = ()
, use undef%hash
if you want to free the storage while %hash
is still in scope.
You can't shrink the number of buckets allocated for the hash using
keys in this way (but you needn't worry about doing this by accident,
as trying has no effect). keys@array
in an lvalue context is a syntax
error.

Starting with Perl 5.14, keys can take a scalar EXPR, which must contain
a reference to an unblessed hash or array. The argument will be
dereferenced automatically. This aspect of keys is considered highly
experimental. The exact behaviour may change in a future version of Perl.