The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Object::Hash

ABSTRACT

Data-Object Hash Class

SYNOPSIS

  use Data::Object::Hash;

  my $hash = Data::Object::Hash->new({1..4});

DESCRIPTION

This package provides routines for operating on Perl 5 hash references.

INHERITANCE

This package inherits behaviors from:

Data::Object::Hash::Base

INTEGRATIONS

This package integrates behaviors from:

Data::Object::Role::Dumpable

Data::Object::Role::Functable

Data::Object::Role::Throwable

LIBRARIES

This package uses type constraints defined by:

Data::Object::Library

METHODS

This package implements the following methods.

clear

  clear() : ArrayObject

The clear method is an alias to the empty method. This method returns a Data::Object::Hash object. This method is an alias to the empty method.

clear example
  # given {1..8}

  $hash->clear; # {}

count

  count() : NumObject

The count method returns the total number of keys defined. This method returns a Data::Object::Number object.

count example
  # given {1..4}

  my $count = $hash->count; # 2

defined

  defined() : NumObject

The defined method returns true if the value matching the key specified in the argument if defined, otherwise returns false. This method returns a Data::Object::Number object.

defined example
  # given {1..8,9,undef}

  $hash->defined(1); # 1; true
  $hash->defined(0); # 0; false
  $hash->defined(9); # 0; false

delete

  delete(Num $arg1) : Any

The delete method returns the value matching the key specified in the argument and returns the value. This method returns a data type object to be determined after execution.

delete example
  # given {1..8}

  $hash->delete(1); # 2

each

  each(CodeRef $arg1, Any @args) : Any

The each method iterates over each element in the hash, executing the code reference supplied in the argument, passing the routine the key and value at the current position in the loop. This method returns a Data::Object::Hash object.

each example
  # given {1..8}

  $hash->each(fun ($key, $value) {
      ...
  });

each_key

  each(CodeRef $arg1, Any @args) : Any

The each_key method iterates over each element in the hash, executing the code reference supplied in the argument, passing the routine the key at the current position in the loop. This method returns a Data::Object::Hash object.

each_key example
  # given {1..8}

  $hash->each_key(fun ($key) {
      ...
  });

each_n_values

  each(Num $arg1, CodeRef $arg2, Any @args) : Any

The each_n_values method iterates over each element in the hash, executing the code reference supplied in the argument, passing the routine the next n values until all values have been seen. This method returns a Data::Object::Hash object.

each_n_values example
  # given {1..8}

  $hash->each_n_values(4, fun (@values) {
      $values[1] # 2
      $values[2] # 4
      $values[3] # 6
      $values[4] # 8
      ...
  });

each_value

  each(CodeRef $arg1, Any @args) : Any

The each_value method iterates over each element in the hash, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop. This method returns a Data::Object::Hash object.

each_value example
  # given {1..8}

  $hash->each_value(fun ($value) {
      ...
  });

empty

  empty() : Object

The empty method drops all elements from the hash. This method returns a Data::Object::Hash object. Note: This method modifies the hash.

empty example
  # given {1..8}

  $hash->empty; # {}

eq

  eq(Any $arg1) : NumObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

eq example
  # given $hash

  $hash->eq; # exception thrown

exists

  exists(Num $arg1) : DoNUm

The exists method returns true if the value matching the key specified in the argument exists, otherwise returns false. This method returns a Data::Object::Number object.

exists example
  # given {1..8,9,undef}

  $hash->exists(1); # 1; true
  $hash->exists(0); # 0; false

filter_exclude

  filter_exclude(Str @args) : HashObject

The filter_exclude method returns a hash reference consisting of all key/value pairs in the hash except for the pairs whose keys are specified in the arguments. This method returns a Data::Object::Hash object.

filter_exclude example
  # given {1..8}

  $hash->filter_exclude(1,3); # {5=>6,7=>8}

filter_include

  filter_include(Str @args) : HashObject

The filter_include method returns a hash reference consisting of only key/value pairs whose keys are specified in the arguments. This method returns a Data::Object::Hash object.

filter_include example
  # given {1..8}

  $hash->filter_include(1,3); # {1=>2,3=>4}

fold

  fold(Str $arg1, HashRef $arg2, HashRef $arg3) : HashObject

The fold method returns a single-level hash reference consisting of key/value pairs whose keys are paths (using dot-notation where the segments correspond to nested hash keys and array indices) mapped to the nested values. This method returns a Data::Object::Hash object.

fold example
  # given {3,[4,5,6],7,{8,8,9,9}}

  $hash->fold; # {'3:0'=>4,'3:1'=>5,'3:2'=>6,'7.8'=>8,'7.9'=>9}

ge

  ge(Any $arg1) : NumObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

ge example
  # given $hash

  $hash->ge; # exception thrown

get

  get(Str $arg1) : Any

The get method returns the value of the element in the hash whose key corresponds to the key specified in the argument. This method returns a data type object to be determined after execution.

get example
  # given {1..8}

  $hash->get(5); # 6

grep

  grep(CodeRef $arg1, Any $arg2) : ArrayObject

The grep method iterates over each key/value pair in the hash, executing the code reference supplied in the argument, passing the routine the key and value at the current position in the loop and returning a new hash reference containing the elements for which the argument evaluated true. This method returns a Data::Object::Hash object.

grep example
  # given {1..4}

  $hash->grep(fun ($value) {
      $value >= 3
  });

  # {3=>5}

gt

  gt(Any $arg1) : NumObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

gt example
  # given $hash

  $hash->gt; # exception thrown
  head() : Any

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

head example
  # given $hash

  $hash->head; # exception thrown

invert

  invert() : Any

The invert method returns the hash after inverting the keys and values respectively. Note, keys with undefined values will be dropped, also, this method modifies the hash. This method returns a Data::Object::Hash object. Note: This method modifies the hash.

invert example
  # given {1..8,9,undef,10,''}

  $hash->invert; # {''=>10,2=>1,4=>3,6=>5,8=>7}

iterator

  iterator() : CodeObject

The iterator method returns a code reference which can be used to iterate over the hash. Each time the iterator is executed it will return the values of the next element in the hash until all elements have been seen, at which point the iterator will return an undefined value. This method returns a Data::Object::Code object.

iterator example
  # given {1..8}

  my $iterator = $hash->iterator;
  while (my $value = $iterator->next) {
      say $value; # 2
  }

join

  join() : StrObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

join example
  # given $hash

  $hash->join; # exception thrown

keys

  keys() : ArrayObject

The keys method returns an array reference consisting of all the keys in the hash. This method returns a Data::Object::Array object.

keys example
  # given {1..8}

  $hash->keys; # [1,3,5,7]

kvslice

  kvslice(Str @args) : HashObject

The kvslice method returns a hash reference containing the elements in the hash at the key(s) specified in the arguments. This method returns a Data::Object::Hash object.

kvslice example
  # given {1..8}

  my $kvslice = $hash->kvslice(1,5); # {1=>2,5=>6}

le

  le(Any $arg1) : NumObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

le example
  # given $hash

  $hash->le; # exception thrown

length

  length() : NumObject

The length method returns the number of keys in the hash. This method return a Data::Object::Number object.

length example
  # given {1..8}

  my $length = $hash->length; # 4

list

  list() : ArrayObject

The list method returns a shallow copy of the underlying hash reference as an array reference. This method return a Data::Object::Array object.

list example
  # given $hash

  my $list = $hash->list;

lookup

  lookup(Str $arg1) : Any

The lookup method returns the value of the element in the hash whose key corresponds to the key specified in the argument. The key can be a string which references (using dot-notation) nested keys within the hash. This method will return undefined if the value is undef or the location expressed in the argument can not be resolved. Please note, keys containing dots (periods) are not handled. This method returns a data type object to be determined after execution.

lookup example
  # given {1..3,{4,{5,6,7,{8,9,10,11}}}}

  $hash->lookup('3.4.7'); # {8=>9,10=>11}
  $hash->lookup('3.4'); # {5=>6,7=>{8=>9,10=>11}}
  $hash->lookup(1); # 2

lt

  lt(Any $arg1) : NumObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

lt example
  # given $hash

  $hash->lt; # exception thrown

map

  map(CodeRef $arg1, Any $arg2) : ArrayObject

The map method iterates over each key/value in the hash, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop and returning a new hash reference containing the elements for which the argument returns a value or non-empty list. This method returns a Data::Object::Hash object.

map example
  # given {1..4}

  $hash->map(sub {
      shift + 1
  });

merge

  merge() : HashObject

The merge method returns a hash reference where the elements in the hash and the elements in the argument(s) are merged. This operation performs a deep merge and clones the datasets to ensure no side-effects. The merge behavior merges hash references only, all other data types are assigned with precendence given to the value being merged. This method returns a Data::Object::Hash object.

merge example
  # given {1..8}

  $hash->merge({7,7,9,9}); # {1=>2,3=>4,5=>6,7=>7,9=>9}

ne

  ne(Any $arg1) : NumObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

ne example
  # given $hash

  $hash->ne; # exception thrown

pairs

  pairs() : ArrayObject

The pairs method is an alias to the pairs_array method. This method returns a Data::Object::Array object. This method is an alias to the pairs_array method.

pairs example
  # given {1..8}

  $hash->pairs; # [[1,2],[3,4],[5,6],[7,8]]

reset

  reset() : HashObject

The reset method returns nullifies the value of each element in the hash. This method returns a Data::Object::Hash object. Note: This method modifies the hash.

reset example
  # given {1..8}

  $hash->reset; # {1=>undef,3=>undef,5=>undef,7=>undef}

reverse

  reverse() : ArrayObject

The reverse method returns a hash reference consisting of the hash's keys and values inverted. Note, keys with undefined values will be dropped. This method returns a Data::Object::Hash object.

reverse example
  # given {1..8,9,undef}

  $hash->reverse; # {8=>7,6=>5,4=>3,2=>1}

self

  self() : Object

The self method returns the calling object (noop).

self example
  # given $hash

  my $self = $hash->self();

set

  set(Str $arg1, Any $arg2) : Any

The set method returns the value of the element in the hash corresponding to the key specified by the argument after updating it to the value of the second argument. This method returns a data type object to be determined after execution.

set example
  # given {1..8}

  $hash->set(1,10); # 10
  $hash->set(1,12); # 12
  $hash->set(1,0); # 0

slice

  slice(Str @args) : ArrayObject

The slice method returns an array reference of the values that correspond to the key(s) specified in the arguments. This method returns a Data::Object::Array object.

slice example
  # given {1..8}

  $hash->slice(1,3); # [2,4]

sort

  sort() : ArrayObject

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

sort example
  # given $hash

  $hash->sort; # exception thrown

tail

  tail() : Any

This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.

tail example
  # given $hash

  $hash->tail; # exception thrown

unfold

  unfold() : HashObject

The unfold method processes previously folded hash references and returns an unfolded hash reference where the keys, which are paths (using dot-notation where the segments correspond to nested hash keys and array indices), are used to created nested hash and/or array references. This method returns a Data::Object::Hash object.

unfold example
  # given {'3:0'=>4,'3:1'=>5,'3:2'=>6,'7.8'=>8,'7.9'=>9}

  $hash->unfold; # {3=>[4,5,6],7,{8,8,9,9}}

values

  values() : ArrayObject

The values method returns an array reference consisting of the values of the elements in the hash. This method returns a Data::Object::Array object.

values example
  # given {1..8}

  $hash->values; # [2,4,6,8]

CREDITS

Al Newkirk, +309

Anthony Brummett, +10

Adam Hopkins, +2

José Joaquín Atria, +1

AUTHOR

Al Newkirk, awncorp@cpan.org

LICENSE

Copyright (C) 2011-2019, Al Newkirk, et al.

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

PROJECT

Wiki

Project

Initiatives

Milestones

Contributing

Issues

SEE ALSO

To get the most out of this distribution, consider reading the following:

Do

Data::Object

Data::Object::Class

Data::Object::ClassHas

Data::Object::Role

Data::Object::RoleHas

Data::Object::Library