MouseX...HashRef(3pm)
π MouseX...HashRef(3pm) π
okay bestie, welcome to MouseX::NativeTraits::HashRef(3pm) β that's the page title, that's the manual name, and yes it's giving "long Perl namespace" but it's literally so useful, i promise π«Ά
β¨ NAME β¨
MouseX::NativeTraits::HashRef β Helper trait for HashRef attributes πΈ
π¦ SYNOPSIS π¦
no cap, this is the whole vibe right here:
package Stuff;
use Mouse;
has 'options' => (
traits => ['Hash'],
is => 'ro',
isa => 'HashRef[Str]',
default => sub { {} },
handles => {
set_option => 'set',
get_option => 'get',
has_no_options => 'is_empty',
num_options => 'count',
delete_option => 'delete',
pairs => 'kv',
},
);
look at that. traits => ['Hash'], a HashRef[Str] type constraint, a default => sub { {} } so it starts empty, and a whole lil handles map turning hash ops into methods on your attribute. iconic behavior fr fr π
π DESCRIPTION π
This module provides a Hash attribute which provides a number of hash-like operations. π
That's it. That's the whole pitch. You get a hash-y attribute and it comes with a whole toolbox of hash-y methods attached. lowkey obsessed.
π PROVIDED METHODS π
These methods are implemented in MouseX::NativeTraits::MethodProvider::HashRef. (yes, that's where they actually live, that's not me being dramatic, that's just where the code is.)
get($key, $key2, $key3...)β Returns values from the hash. In list context return a list of values in the hash for the given keys. In scalar context returns the value for the last key specified. π
bestie, watch your context here β list context and scalar context do different things. that's the whole note. π
set($key => $value, $key2 => $value2...)β Sets the elements in the hash to the given values. β¨delete($key, $key2, $key3...)β Removes the elements with the given keys. bye bye π¦exists($key)β Returns true if the given key is present in the hash. πdefined($key)β Returns true if the value of a given key is defined. (different question thanexists, and yes both of them are here, slay) πkeysβ Returns the list of keys in the hash. πsorted_keysβ Returns the list of sorted keys in the hash. π but in order β¨valuesβ Returns the list of values in the hash. πΈkvβ Returns the key/value pairs in the hash as an array of array references.
for my $pair ( $object->options->pairs ) {
print "$pair->[0] = $pair->[1]\n";
}
see how $pair->[0] is the key and $pair->[1] is the value? yeah. array of arrayrefs. that's the shape. π«Ά
elementsβ Returns the key/value pairs in the hash as a flattened list. (same pairs, but squished flat, no arrayrefs, just vibes) πclearβ Resets the hash to an empty value, like%hash = (). gone. poof. πcountβ Returns the number of elements in the hash. Also useful for not empty:has_options => 'count'. π not empty!! we love a truthy count!!is_emptyβ If the hash is populated, returns false. Otherwise, returns true. πaccessorβ If passed one argument, returns the value of the specified key. If passed two arguments, sets the value of the specified key. one arg = read, two args = write. main character duality π
π METHODS π
short list, big energy:
metamethod_provider_classhelper_type
that's the whole list, no notes, no extra detail in the original, so no extra detail from me. periodt.
π SEE ALSO π
MouseX::NativeTraits π β go read that one next bestie, it's the parent vibe.
and that's the page!! πβ¨ this was perl v5.34.0, dated 2022-06-16 β so if you're running something way newer, still go off, but that's the version this manual page shipped with. mwah π
π¦ where this page comes from
- debian bookworm: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms