Sub::HandlesVia::HandlerLibrary::Hash(3pm) | User Contributed Perl Documentation | Sub::HandlesVia::HandlerLibrary::Hash(3pm) |
Sub::HandlesVia::HandlerLibrary::Hash - library of hash-related 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', }, ); }
This is a library of methods for Sub::HandlesVia.
Arguments: Str, Optional[Any].
Acts like "get" if given just a key, or "set" if given a key and a value.
Returns the hash in list context.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); my %hash = $object->my_all;
Empties the hash.
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
Returns the number of keys in the hash.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); say $object->my_count; ## ==> 2
Arguments: Str.
Indicates whether a value exists and is defined in the hashref by its key.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); say $object->my_defined( 'foo' ); ## ==> 1
Removes a value from the hashref by its key.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); $object->my_delete( 'foo' ); say exists $object->attr->{foo}; ## ==> false
Arguments: CodeRef|RegexpRef.
Removes values from the hashref by matching keys against a coderef or regexp.
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 }
Returns the hash in list context.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); my %hash = $object->my_elements;
Arguments: Str.
Indicates whether a value exists in the hashref by its key.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); say $object->my_exists( 'foo' ); ## ==> true say $object->my_exists( 'baz' ); ## ==> false
Arguments: CodeRef.
Chainable method which calls the coderef for each key in the hash, passing just the key to the coderef.
Arguments: CodeRef.
Chainable method which calls the coderef for each key in the hash, passing the key and value to the coderef.
Arguments: CodeRef.
Chainable method which calls the coderef for each value in the hash, passing just the value to the coderef.
Returns a value from the hashref by its key.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); say $object->my_get( 'bar' ); ## ==> 1
Returns true iff there are no keys in the hash.
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
Returns the list of keys in the hash.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); # says 'foo' and 'bar' in an unpredictable order say for $object->my_keys;
Returns a list of arrayrefs, where each arrayref is a key-value pair.
Resets the attribute to its default value, or an empty hashref if it has no default.
Given a key and value, adds the key to the hashref with the given value.
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
Creates a new hashref with the same keys and values as the original.
Returns an alphabetically sorted list of keys in the hash.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); # says 'bar' then 'foo' say for $object->my_sorted_keys;
Returns the list of values in the hash.
my $object = My::Class->new( attr => { foo => 0, bar => 1 } ); # says '0' and '1' in an unpredictable order say for $object->my_values;
Please report any bugs to <https://github.com/tobyink/p5-sub-handlesvia/issues>.
Sub::HandlesVia.
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.
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.
2023-04-09 | perl v5.36.0 |