The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

HV Handling A HV structure represents a Perl hash. It consists mainly of an array of pointers, each of which points to a linked list of HE structures. The array is indexed by the hash function of the key, so each linked list represents all the hash entries with the same hash value. Each HE contains a pointer to the actual value, plus a pointer to a HEK structure which holds the key and hash value.

Stores an SV in a hash. The hash key is specified as key and the absolute value of klen is the length of the key. If klen is negative the key is assumed to be in UTF-8-encoded Unicode. The hash parameter is the precomputed hash value; if it is zero then Perl will compute it.

The return value will be NULL if the operation failed or if the value did not need to be actually stored within the hash (as in the case of tied hashes). Otherwise it can be dereferenced to get the original SV*. Note that the caller is responsible for suitably incrementing the reference count of val before the call, and decrementing it if the function returned NULL. Effectively a successful hv_store takes ownership of one reference to val. This is usually what you want; a newly created SV has a reference count of one, so if all your code does is create SVs then store them in a hash, hv_store will own the only reference to the new SV, and your code doesn't need to do anything further to tidy up. hv_store is not implemented as a call to hv_store_ent, and does not create a temporary SV for the key, so if your key data is not already in SV form then use hv_store in preference to hv_store_ent.

See "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more information on how to use this function on tied hashes.

Stores val in a hash. The hash key is specified as key. The hash parameter is the precomputed hash value; if it is zero then Perl will compute it. The return value is the new hash entry so created. It will be NULL if the operation failed or if the value did not need to be actually stored within the hash (as in the case of tied hashes). Otherwise the contents of the return value can be accessed using the He? macros described here. Note that the caller is responsible for suitably incrementing the reference count of val before the call, and decrementing it if the function returned NULL. Effectively a successful hv_store_ent takes ownership of one reference to val. This is usually what you want; a newly created SV has a reference count of one, so if all your code does is create SVs then store them in a hash, hv_store will own the only reference to the new SV, and your code doesn't need to do anything further to tidy up. Note that hv_store_ent only reads the key; unlike val it does not take ownership of it, so maintaining the correct reference count on key is entirely the caller's responsibility. The reason it does not take ownership, is that key is not used after this function returns, and so can be freed immediately. hv_store is not implemented as a call to hv_store_ent, and does not create a temporary SV for the key, so if your key data is not already in SV form then use hv_store in preference to hv_store_ent.

See "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more information on how to use this function on tied hashes.

Returns a boolean indicating whether the specified hash key exists. The absolute value of klen is the length of the key. If klen is negative the key is assumed to be in UTF-8-encoded Unicode.

Returns the SV which corresponds to the specified key in the hash. The absolute value of klen is the length of the key. If klen is negative the key is assumed to be in UTF-8-encoded Unicode. If lval is set then the fetch will be part of a store. This means that if there is no value in the hash associated with the given key, then one is created and a pointer to it is returned. The SV* it points to can be assigned to. But always check that the return value is non-null before dereferencing it to an SV*.

See "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more information on how to use this function on tied hashes.

Returns a boolean indicating whether the specified hash key exists. hash can be a valid precomputed hash value, or 0 to ask for it to be computed.

Returns the hash entry which corresponds to the specified key in the hash. hash must be a valid precomputed hash number for the given key, or 0 if you want the function to compute it. IF lval is set then the fetch will be part of a store. Make sure the return value is non-null before accessing it. The return value when hv is a tied hash is a pointer to a static location, so be sure to make a copy of the structure if you need to store it somewhere.

See "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more information on how to use this function on tied hashes.

Evaluates the hash in scalar context and returns the result.

When the hash is tied dispatches through to the SCALAR method, otherwise returns a mortal SV containing the number of keys in the hash.

Note, prior to 5.25 this function returned what is now returned by the hv_bucket_ratio() function.

If the hash is tied dispatches through to the SCALAR tied method, otherwise if the hash contains no keys returns 0, otherwise returns a mortal sv containing a string specifying the number of used buckets, followed by a slash, followed by the number of available buckets.

This function is expensive, it must scan all of the buckets to determine which are used, and the count is NOT cached. In a large hash this could be a lot of buckets.

Deletes a key/value pair in the hash. The value's SV is removed from the hash, made mortal, and returned to the caller. The absolute value of klen is the length of the key. If klen is negative the key is assumed to be in UTF-8-encoded Unicode. The flags value will normally be zero; if set to G_DISCARD then NULL will be returned. NULL will also be returned if the key is not found.

Deletes a key/value pair in the hash. The value SV is removed from the hash, made mortal, and returned to the caller. The flags value will normally be zero; if set to G_DISCARD then NULL will be returned. NULL will also be returned if the key is not found. hash can be a valid precomputed hash value, or 0 to ask for it to be computed.

A specialised version of "newHVhv" for copying %^H. ohv must be a pointer to a hash (which may have %^H magic, but should be generally non-magical), or NULL (interpreted as an empty hash). The content of ohv is copied to a new hash, which has the %^H-specific magic added to it. A pointer to the new hash is returned.

Frees all the elements of a hash, leaving it empty. The XS equivalent of %hash = (). See also "hv_undef".

See "av_clear" for a note about the hash possibly being invalid on return.

Clears any placeholders from a hash. If a restricted hash has any of its keys marked as readonly and the key is subsequently deleted, the key is not actually deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags it so it will be ignored by future operations such as iterating over the hash, but will still allow the hash to have a value reassigned to the key at some future point. This function clears any such placeholder keys from the hash. See Hash::Util::lock_keys() for an example of its use.

Undefines the hash. The XS equivalent of undef(%hash).

As well as freeing all the elements of the hash (like hv_clear()), this also frees any auxiliary data and storage associated with the hash.

See "av_clear" for a note about the hash possibly being invalid on return.

Returns the number of hash buckets that happen to be in use.

This function is wrapped by the macro HvFILL.

As of perl 5.25 this function is used only for debugging purposes, and the number of used hash buckets is not in any way cached, thus this function can be costly to execute as it must iterate over all the buckets in the hash.

Prepares a starting point to traverse a hash table. Returns the number of keys in the hash, including placeholders (i.e. the same as HvTOTALKEYS(hv)). The return value is currently only meaningful for hashes without tie magic.

NOTE: Before version 5.004_65, hv_iterinit used to return the number of hash buckets that happen to be in use. If you still need that esoteric value, you can get it through the macro HvFILL(hv).

Adds a name to a stash's internal list of effective names. See "hv_ename_delete".

This is called when a stash is assigned to a new location in the symbol table.

Removes a name from a stash's internal list of effective names. If this is the name returned by HvENAME, then another name in the list will take its place (HvENAME will use it).

This is called when a stash is deleted from the symbol table.

Returns entries from a hash iterator. See "hv_iterinit".

You may call hv_delete or hv_delete_ent on the hash entry that the iterator currently points to, without losing your place or invalidating your iterator. Note that in this case the current entry is deleted from the hash with your iterator holding the last reference to it. Your iterator is flagged to free the entry on the next call to hv_iternext, so you must not discard your iterator immediately else the entry will leak - call hv_iternext to trigger the resource deallocation.

Returns entries from a hash iterator. See "hv_iterinit" and "hv_iternext". The flags value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is set the placeholders keys (for restricted hashes) will be returned in addition to normal keys. By default placeholders are automatically skipped over. Currently a placeholder is implemented with a value that is &PL_sv_placeholder. Note that the implementation of placeholders and restricted hashes may change, and the implementation currently is insufficiently abstracted for any change to be tidy.

Returns the key from the current position of the hash iterator. See "hv_iterinit".

Returns the key as an SV* from the current position of the hash iterator. The return value will always be a mortal copy of the key. Also see "hv_iterinit".

Returns the value from the current position of the hash iterator. See "hv_iterkey".

Performs an hv_iternext, hv_iterkey, and hv_iterval in one operation.

Adds magic to a hash. See "sv_magic".

Generates and returns a HV * representing the content of a refcounted_he chain. flags is currently unused and must be zero.

Search along a refcounted_he chain for an entry with the key specified by keypv and keylen. If flags has the REFCOUNTED_HE_KEY_UTF8 bit set, the key octets are interpreted as UTF-8, otherwise they are interpreted as Latin-1. hash is a precomputed hash of the key string, or zero if it has not been precomputed. Returns a mortal scalar representing the value associated with the key, or &PL_sv_placeholder if there is no value associated with the key.

Like "refcounted_he_fetch_pvn", but takes a nul-terminated string instead of a string/length pair.

Like "refcounted_he_fetch_pvn", but takes a Perl scalar instead of a string/length pair.

Creates a new refcounted_he. This consists of a single key/value pair and a reference to an existing refcounted_he chain (which may be empty), and thus forms a longer chain. When using the longer chain, the new key/value pair takes precedence over any entry for the same key further along the chain.

The new key is specified by keypv and keylen. If flags has the REFCOUNTED_HE_KEY_UTF8 bit set, the key octets are interpreted as UTF-8, otherwise they are interpreted as Latin-1. hash is a precomputed hash of the key string, or zero if it has not been precomputed.

value is the scalar value to store for this key. value is copied by this function, which thus does not take ownership of any reference to it, and later changes to the scalar will not be reflected in the value visible in the refcounted_he. Complex types of scalar will not be stored with referential integrity, but will be coerced to strings. value may be either null or &PL_sv_placeholder to indicate that no value is to be associated with the key; this, as with any non-null value, takes precedence over the existence of a value for the key further along the chain.

parent points to the rest of the refcounted_he chain to be attached to the new refcounted_he. This function takes ownership of one reference to parent, and returns one reference to the new refcounted_he.

Like "refcounted_he_new_pvn", but takes a nul-terminated string instead of a string/length pair.

Like "refcounted_he_new_pvn", but takes a Perl scalar instead of a string/length pair.

Decrements the reference count of a refcounted_he by one. If the reference count reaches zero the structure's memory is freed, which (recursively) causes a reduction of its parent refcounted_he's reference count. It is safe to pass a null pointer to this function: no action occurs in this case.

Increment the reference count of a refcounted_he. The pointer to the refcounted_he is also returned. It is safe to pass a null pointer to this function: no action occurs and a null pointer is returned.

Returns the label attached to a cop, and stores its length in bytes into *len. Upon return, *flags will be set to either SVf_UTF8 or 0.

Alternatively, use the macro "CopLABEL_len_flags"; or if you don't need to know if the label is UTF-8 or not, the macro "CopLABEL_len"; or if you additionally dont need to know the length, "CopLABEL".

Save a label into a cop_hints_hash. You need to set flags to SVf_UTF8 for a UTF-8 label. Any other flag is ignored.

Check that a hash is in an internally consistent state.