Hash-DotPath

 view release on metacpan or  search on metacpan

lib/Hash/DotPath.pm  view on Meta::CPAN

package Hash::DotPath;
$Hash::DotPath::VERSION = '0.004';
use Modern::Perl;
use Moose;
use namespace::autoclean;
use Kavorka 'method';
use Data::Printer alias => 'pdump';
use Hash::Merge;
use Util::Medley::Hash;

with
  'Util::Medley::Roles::Attributes::Hash',
  'Util::Medley::Roles::Attributes::List',
  'Util::Medley::Roles::Attributes::Logger',
  'Util::Medley::Roles::Attributes::String';

########################################################

=head1 NAME

Hash::DotPath - Class for manipulating hashes via dot path notation.

=head1 VERSION

version 0.004

=cut

########################################################

=head1 SYNOPSIS

  $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;  
  
=cut

=head1 ARRAY vs HASH vivification

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'); 



( run in 0.866 second using v1.01-cache-2.11-cpan-437f7b0c052 )