Sub::Ha...ry::Hash(3)

πŸ’– Sub::HandlesVia::HandlerLibrary::Hash πŸ’–

okay bestie, so this page is Sub::HandlesVia::HandlerLibrary::Hash, and it's part of the User Contributed Perl Documentation collection, iykyk. It's the whole entire library of hash-related methods for Sub::HandlesVia, and honestly? it's giving main character energy for your hash attributes. πŸŽ€


✨ SYNOPSIS ✨

here's the vibe. you got a class, you got a HashRef attribute, and you're about to hand it all the hash methods:

package My::Class {
  use Moo;
  use Sub::HandlesVia;
  use Types::Standard 'HashRef';
  has attr => (
    is => 'rwp',
    isa => HashRef,
    handles_via => 'Hash',
    handles => {
      'my_accessor' => 'accessor',
      'my_all' => 'all',
      'my_clear' => 'clear',
      'my_count' => 'count',
      'my_defined' => 'defined',
      'my_delete' => 'delete',
      'my_delete_where' => 'delete_where',
      'my_elements' => 'elements',
      'my_exists' => 'exists',
      'my_for_each_key' => 'for_each_key',
      'my_for_each_pair' => 'for_each_pair',
      'my_for_each_value' => 'for_each_value',
      'my_get' => 'get',
      'my_is_empty' => 'is_empty',
      'my_keys' => 'keys',
      'my_kv' => 'kv',
      'my_reset' => 'reset',
      'my_set' => 'set',
      'my_shallow_clone' => 'shallow_clone',
      'my_sorted_keys' => 'sorted_keys',
      'my_values' => 'values',
    },
  );
}

look at all those handles. she's booked and busy. πŸ’…


🌸 DESCRIPTION 🌸

literally what it says on the tin: this is a library of methods for Sub::HandlesVia. that's the whole description, no notes. ✨


πŸ¦‹ DELEGATABLE METHODS πŸ¦‹

every single one of these is delegatable, bestie. we're going through all of them, no skips. πŸ’œ

🌈 accessor( $key, $value? )

Arguments: Str, Optional[Any].

so basically this one's a shapeshifter: give it just a key and it acts like get, or give it a key and a value and it acts like set. versatile queen. πŸ‘‘

🌈 all()

returns the hash in list context. that's it, that's the method.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
my %hash = $object->my_all;

🌈 clear()

empties the hash. poof, gone, nothing left. πŸ’€

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
$object->my_clear;
say exists $object->attr->{foo}; ## ==> false
say exists $object->attr->{bar}; ## ==> false

🌈 count()

returns the number of keys in the hash. counting is self-care fr fr.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
say $object->my_count; ## ==> 2

🌈 defined( $key )

Arguments: Str.

tells you whether a value exists and is defined in the hashref, by its key. two boxes checked, periodt.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
say $object->my_defined( 'foo' ); ## ==> 1

🌈 delete( $key )

removes a value from the hashref by its key. bye babe, we won't miss you. 🫢

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
$object->my_delete( 'foo' );
say exists $object->attr->{foo}; ## ==> false

🌈 delete_where( $match )

Arguments: CodeRef|RegexpRef.

removes values from the hashref by matching the keys against either a coderef or a regexp. you pick your fighter. πŸ”₯

my $object = My::Class->new( attr => { foo => 0, bar => 1, baz => 2 } );
$object->my_delete_where( sub { $_ eq 'foo' or $_ eq 'bar' } );
say Dumper( $object->attr ); ## ==> { baz => 2 }

my $object2 = My::Class->new( attr => { foo => 0, bar => 1, baz => 2 } );
$object2->my_delete_where( qr/^b/ );
say Dumper( $object2->attr ); ## ==> { foo => 0 }

🌈 elements()

returns the hash in list context. (yes, same energy as all. they're twins, don't question it.)

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
my %hash = $object->my_elements;

🌈 exists( $key )

Arguments: Str.

tells you whether a value exists in the hashref by its key. no judgement about undef here, that's defined's whole thing.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
say $object->my_exists( 'foo' ); ## ==> true
say $object->my_exists( 'baz' ); ## ==> false

🌈 for_each_key( $coderef )

Arguments: CodeRef.

a chainable method (yes bestie, it returns itself so you can keep the party going πŸŽ‰) which calls the coderef once for each key in the hash, passing just the key to the coderef. the key will also be available as $_.

🌈 for_each_pair( $coderef )

Arguments: CodeRef.

chainable, and this time it calls the coderef for each key in the hash passing the key and the value to the coderef. two-for-one deal, iconic.

🌈 for_each_value( $coderef )

Arguments: CodeRef.

chainable, calls the coderef for each value in the hash, passing just the value. the value will also be available as $_.

🌈 get( $key )

returns a value from the hashref by its key. the classic. the original. we love her.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
say $object->my_get( 'bar' ); ## ==> 1

🌈 is_empty()

returns true iff there are no keys in the hash. watch the glow-up from false to true in the example, it's a whole arc:

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
say $object->my_is_empty; ## ==> false
$object->_set_attr( {} );
say $object->my_is_empty; ## ==> true

🌈 keys()

returns the list of keys in the hash. heads up: the order is unpredictable, so don't go building your whole personality on it. πŸ™ˆ

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
# says 'foo' and 'bar' in an unpredictable order
say for $object->my_keys;

🌈 kv()

returns a list of arrayrefs, where each arrayref is a key-value pair. cute little couples. πŸ’ž

🌈 reset()

resets the attribute to its default value, or to an empty hashref if it has no default. fresh start, clean slate, new era. ✨

🌈 set( $key, $value, ... )

given a key and a value, adds the key to the hashref with the given value. (you can keep going with more pairs, look at the example, she's stacking them.)

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
$object->my_set( bar => 2, baz => 1 );
say $object->attr->{foo}; ## ==> 0
say $object->attr->{baz}; ## ==> 1
say $object->attr->{bar}; ## ==> 2

🌈 shallow_clone()

creates a new hashref with the same keys and values as the original. a copy that ate and left no crumbs. πŸ’…

🌈 sorted_keys()

returns an alphabetically sorted list of keys in the hash. finally, some order in this chaotic world.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
# says 'bar' then 'foo'
say for $object->my_sorted_keys;

🌈 values()

returns the list of values in the hash. same deal as keys, the order is unpredictable, no cap.

my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
# says '0' and '1' in an unpredictable order
say for $object->my_values;

πŸ’€ BUGS πŸ’€

found a bug? not the vibe, but please report it, bestie, we're listening:

<https://github.com/tobyink/p5-sub-handlesvia/issues>


πŸ‘€ SEE ALSO πŸ‘€

  • Sub::HandlesVia.

πŸ‘‘ AUTHOR πŸ‘‘

Toby Inkster <tobyink@cpan.org>.


This software is copyright (c) 2020, 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


⚠️ DISCLAIMER OF WARRANTIES ⚠️

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

and that's the whole page, bestie, you're all caught up. πŸ’– this was Sub::HandlesVia::HandlerLibrary::Hash from perl v5.42.0, dated 2026-02-11 β€” go forth and slay your hashes. πŸŒˆβœ¨πŸ¦‹

πŸ“¦ where this page comes from