Hash::Flatten(3pm) | User Contributed Perl Documentation | Hash::Flatten(3pm) |
Hash::Flatten - flatten/unflatten complex data hashes
# Exported functions use Hash::Flatten qw(:all); $flat_hash = flatten($nested_hash); $nested_hash = unflatten($flat_hash); # OO interface my $o = new Hash::Flatten({ HashDelimiter => '->', ArrayDelimiter => '=>', OnRefScalar => 'warn', }); $flat_hash = $o->flatten($nested_hash); $nested_hash = $o->unflatten($flat_hash);
Converts back and forth between a nested hash structure and a flat hash of delimited key-value pairs. Useful for protocols that only support key-value pairs (such as CGI and DBMs).
$nested = { 'x' => 1, 'y' => { 'a' => 2, 'b' => 3 }, 'z' => [ 'a', 'b', 'c' ] } $flat = flatten($nested); use Data::Dumper; print Dumper($flat); $VAR1 = { 'y.a' => 2, 'x' => 1, 'y.b' => 3, 'z:0' => 'a', 'z:1' => 'b', 'z:2' => 'c' };
The "\%options" hashref can be used to override the default behaviour (see "OPTIONS").
By default references to references, and references to scalars, are followed silently.
WARNING: If your structure has keys that contain the delimiter characters, it will not be possible to unflatten the structure correctly.
Any blessings will be discarded during flattening, so that if you flatten an object you must re-bless() it on unflattening.
Note that there is no delimiter for scalar references, or references to references. If your structure to be flattened contains scalar, or reference, references these will be followed by default, i.e. "'foo' => \\\\\\$foo" will be collapsed to "'foo' => $foo". You can override this behaviour using the OnRefScalar and OnRefRef constructor option.
Recursive structures are detected and cause a fatal error.
The perlmonks site has a helpful introduction to when and why you might want to flatten a hash: http://www.perlmonks.org/index.pl?node_id=234186
$Id: Flatten.pm,v 1.19 2009/05/09 12:42:02 jamiel Exp $
John Alden & P Kent <cpan _at_ bbc _dot_ co _dot_ uk>
(c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
2022-12-07 | perl v5.36.0 |