Hash::DotPath - Class for manipulating hashes via dot path notation.
version 0.004
$dot = Hash::DotPath->new; $dot = Hash::DotPath->new(\%myhash); $dot = Hash::DotPath->new(\%myhash, delimiter => '~'); $val = $dot->get('foo.bar'); $val = $dot->get('biz.baz.0.zoo'); $dot->set('foo', 'bar'); $dot->set('cats.0', 'calico'); $dot->delete('foo'); $newObj = $dot->merge({ biz => 'baz' }); $newObj = $dot->merge({ biz => 'other' }, 'RIGHT'); %hash = $dot->toHash; $href = $dot->toHashRef;
When assigning a value to a path where a non-existent segment of the path is an integer, an array reference will be vivified at that position. If you wish to have a hash reference in its place, you must instantiate it manually in advance. For example:
# Assuming biz isn't defined yet, this will set biz to an array reference. $dot = Hash::DotPath->new; $dot->set('biz.0', 'baz'); Data::Printer::p($dot->toHashRef); { biz [ [0] "baz" ] } # In order to set biz to a hash reference you must instantiate it first. $dot->set('biz', {}); $dot->set('biz.0', 'baz'); Data::Printer::p($dot->toHashRef); { biz { 0 "baz" } }
The delimiter to use when analyzing a dot path.
Default: "."
Deletes an element at the specified path. Returns the value of the element that was deleted.
$val = $dot->delete('foo.bar'); $val = $dot->delete('biz.0.baz');
Dot-path of the element you wish to delete.
Gets an element at the specified path. Returns 'Any'.
$val = $dot->get('foo.bar'); $val = $dot->get('biz.0.baz');
Dot-path of the element you wish to get.
Determines if an element exists at the given path. Returns 'Bool'.
$bool = $dot->exists('foo.bar'); $bool = $dot->exists('biz.0.baz');
Merges the provided dot-path object or hashref with the object. You indicate which hash has precedence by providing the 'overwrite' arg.
$newDot = $dot->merge({foo => 'bar'}, [0|1]); $dot2 = Hash::DotPath->new(biz => 'baz'); $newDot = $dot->merge($dot2, [0|1]);
Hashref you wish to merge into the dot-path object.
Indicates which hash has precedence over the other. A true value means the element passed in will overwrite any pre-existing elements. A false value will preserve existing elements and just merge the new ones in.
Default: 1
Sets an element at the specified path. Returns the value that was passed in.
$val = $dot->set('foo.bar', 'abc'); $val = $dot->set('biz.0.baz', 'def');
Dot-path of the element you wish to set.
Value you wish to set at the given path.
Returns a hash version of the object.
%hash = $dot->toHash;
Returns a hashref version of the object.
$href = $dot->toHashRef;
To install Hash::DotPath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Hash::DotPath
CPAN shell
perl -MCPAN -e shell install Hash::DotPath
For more information on module installation, please visit the detailed CPAN module installation guide.