NAME

List::Objects::WithUtils::Role::Hash - Hash manipulation methods

SYNOPSIS

## Via List::Objects::WithUtils::Hash ->
use List::Objects::WithUtils 'hash';

my $hash = hash(foo => 'bar');

$hash->set(
  foo => 'baz',
  pie => 'tasty',
);

my @matches = $hash->keys->grep(sub {
  $_[0] =~ /foo/
})->all;

my $pie = $hash->get('pie')
  if $hash->exists('pie');

for my $pair ( $hash->kv->all ) {
  my ($key, $val) = @$pair;
}

## As a Role ->
use Role::Tiny::With;
with 'List::Objects::WithUtils::Role::Hash';

DESCRIPTION

A Role::Tiny role defining methods for creating and manipulating HASH-type objects.

new

Constructs a new HASH-type object.

export

my %hash = $hash->export;

Returns a raw key/value list.

array_type

The class name of list/array-type objects that will be constructed from the results of list-producing methods.

Defaults to List::Objects::WithUtils::Array.

Subclasses can override array_type to produce different types of array objects; the method can also be queried to find out what kind of array object will be returned:

my $type = $hash->array_type;

clear

Clears the current hash entirely.

is_empty

Returns boolean true if the hash has no keys.

defined

if ( $hash->defined($key) ) { ... }

Returns boolean true if the key has a defined value.

exists

if ( $hash->exists($key) ) { ... }

Returns boolean true if the key exists.

get

my $val  = $hash->get($key);
my @vals = $hash->get(@keys)->all;

Retrieves a key or list of keys from the hash.

If we're taking a slice (multiple keys were specified), values are returned as an "array_type" object. (See "sliced" if you'd rather generate a new hash.)

sliced

my $newhash = $hash->slice(@keys);

Returns a new hash object built from the specified set of keys.

(See "get" if you only need the values.)

keys

my @keys = $hash->keys->all;

Returns the list of keys in the hash as an "array_type" object.

values

my @vals = $hash->values->all;

Returns the list of values in the hash as an "array_type" object.

kv

for my $pair ($hash->kv->all) {
  my ($key, $val) = @$pair;
}

Returns an "array_type" object containing the key/value pairs in the HASH, each of which is a two-element ARRAY.

set

$hash->set(
  key1 => $val,
  key2 => $other,
)

Sets keys in the hash.

Returns an "array_type" object containing the new values.

delete

$hash->delete( @keys );

Deletes keys from the hash.

Returns an "array_type" object containing the deleted values.

SEE ALSO

List::Objects::WithUtils

Data::Perl

AUTHOR

Jon Portnoy <avenj@cobaltirc.org>

Portions of this code are derived from Data::Perl by Matthew Phillips (CPAN: MATTP), haarg et al

Licensed under the same terms as Perl.