NAME
Hash::Fold - flatten and unflatten nested hashrefs
SYNOPSIS
use Hash::Fold qw(flatten unflatten);
my $object = bless { foo => 'bar' };
my $nested = {
foo => $object,
baz => {
a => 'b',
c => [ 'd', { e => 'f' }, 42 ],
},
};
my $flattened = flatten($nested);
is_deeply $flattened, {
'baz.a' => 'b',
'baz.c.0' => 'd',
'baz.c.1.e' => 'f',
'baz.c.2' => 42,
'foo' => $object,
};
my $roundtrip = unflatten($flattened);
is_deeply $roundtrip, $nested;
DESCRIPTION
This module provides functional and OO interfaces which allow hashrefs containing nested values to be flattened into single-level hashrefs (e.g. with dotted keys) and unflattened.
EXPORTS
Nothing by default. The following functions can be imported.
flatten
Takes a nested hashref and returns a single-level hashref with (by default) dotted keys.
Unblessed arrayrefs and unblessed hashrefs are traversed. All other values (e.g. strings, regexps, objects &c.) are treated as terminals and passed through verbatim.
fold
Provided as an alias for "flatten".
unflatten
Takes a flattened hashref and returns the corresponding nested hashref.
unfold
Provided as an alias for "unflatten".
VERSION
0.0.2
SEE ALSO
AUTHOR
chocolateboy <chocolate@cpan.org>
COPYRIGHT
Copyright (c) 2014, chocolateboy.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.