MouseX...HashRef(3pm)

πŸ“¦
version 1.09 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 than exists, 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:

  • meta
  • method_provider_class
  • helper_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