| 1 |
############################################################################### |
| 2 |
# Purpose : Flatten/Unflatten nested data structures to/from key-value form |
| 3 |
# Author : John Alden |
| 4 |
# Created : Feb 2002 |
| 5 |
# CVS : $Id: Flatten.pm,v 1.19 2009/05/09 12:42:02 jamiel Exp $ |
| 6 |
############################################################################### |
| 7 |
|
| 8 |
package Hash::Flatten; |
| 9 |
|
| 10 |
use strict; |
| 11 |
use Exporter; |
| 12 |
use Carp; |
| 13 |
|
| 14 |
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION); |
| 15 |
@ISA = qw(Exporter); |
| 16 |
@EXPORT_OK = qw(flatten unflatten); |
| 17 |
%EXPORT_TAGS = ('all' => \@EXPORT_OK); |
| 18 |
$VERSION = ('$Revision: 1.19 $' =~ /([\d\.]+)/)[0]; |
| 19 |
|
| 20 |
use constant DEFAULT_HASH_DELIM => '.'; |
| 21 |
use constant DEFAULT_ARRAY_DELIM => ':'; |
| 22 |
|
| 23 |
#Check if we need to support overloaded stringification |
| 24 |
use constant HAVE_OVERLOAD => eval { |
| 25 |
require overload; |
| 26 |
}; |
| 27 |
|
| 28 |
sub new |
| 29 |
{ |
| 30 |
my ($class, $options) = @_; |
| 31 |
$options = {} unless ref $options eq 'HASH'; |
| 32 |
my $self = { |
| 33 |
%$options |
| 34 |
}; |
| 35 |
|
| 36 |
#Defaults |
| 37 |
$self->{HashDelimiter} ||= DEFAULT_HASH_DELIM; |
| 38 |
$self->{ArrayDelimiter} ||= DEFAULT_ARRAY_DELIM; |
| 39 |
$self->{EscapeSequence} = "\\" unless(defined $self->{EscapeSequence} && length($self->{EscapeSequence}) > 0); |
| 40 |
$self->{EscapeSequence} = undef if($self->{DisableEscapes}); |
| 41 |
|
| 42 |
#Sanity check: delimiters don't contain escape sequence |
| 43 |
croak("Hash delimiter cannot contain escape sequence") if($self->{HashDelimiter} =~ /\Q$self->{EscapeSequence}\E/); |
| 44 |
croak("Array delimiter cannot contain escape sequence") if($self->{ArrayDelimiter} =~ /\Q$self->{EscapeSequence}\E/); |
| 45 |
|
| 46 |
TRACE(__PACKAGE__." constructor - $self"); |
| 47 |
return bless($self, $class); |
| 48 |
} |
| 49 |
|
| 50 |
sub flatten |
| 51 |
{ |
| 52 |
#Convert functional to OO with default ctor |
| 53 |
if(ref $_[0] ne __PACKAGE__) { |
| 54 |
return __PACKAGE__->new($_[1])->flatten($_[0]); |
| 55 |
} |
| 56 |
|
| 57 |
my ($self, $hashref) = @_; |
| 58 |
die("1st arg must be a hashref") unless(UNIVERSAL::isa($hashref, 'HASH')); |
| 59 |
|
| 60 |
my $delim = { |
| 61 |
'HASH' => $self->{HashDelimiter}, |
| 62 |
'ARRAY' => $self->{ArrayDelimiter} |
| 63 |
}; |
| 64 |
$self->{RECURSE_CHECK} = {}; |
| 65 |
my @flat = $self->_flatten_hash_level($hashref,$delim); |
| 66 |
my %flat_hash = map {$_->[0], $_->[1]} @flat; |
| 67 |
return \%flat_hash; |
| 68 |
} |
| 69 |
|
| 70 |
sub unflatten |
| 71 |
{ |
| 72 |
#Convert functional to OO with default ctor |
| 73 |
if(ref $_[0] ne __PACKAGE__) { |
| 74 |
return __PACKAGE__->new($_[1])->unflatten($_[0]); |
| 75 |
} |
| 76 |
|
| 77 |
my ($self, $hashref) = @_; |
| 78 |
die("1st arg must be a hashref") unless(UNIVERSAL::isa($hashref, 'HASH')); |
| 79 |
|
| 80 |
my $delim = { |
| 81 |
'HASH' => $self->{HashDelimiter}, |
| 82 |
'ARRAY' => $self->{ArrayDelimiter} |
| 83 |
}; |
| 84 |
|
| 85 |
my $regexp = '((?:' . quotemeta($delim->{'HASH'}) . ')|(?:' . quotemeta($delim->{'ARRAY'}) . '))'; |
| 86 |
if($self->{EscapeSequence}) { |
| 87 |
$regexp = '(?<!'.quotemeta($self->{EscapeSequence}).')'.$regexp; #Use negative look behind |
| 88 |
} |
| 89 |
TRACE("regex = /$regexp/"); |
| 90 |
|
| 91 |
my %expanded; |
| 92 |
foreach my $key (keys %$hashref) |
| 93 |
{ |
| 94 |
my $value = $hashref->{$key}; |
| 95 |
my @levels = split(/$regexp/, $key); |
| 96 |
|
| 97 |
my $finalkey = $self->_unescape((scalar(@levels) % 2 ? pop(@levels) : ''), $self->{EscapeSequence}); |
| 98 |
my $ptr = \%expanded; |
| 99 |
while (@levels >= 2) |
| 100 |
{ |
| 101 |
my $key = $self->_unescape(shift(@levels), $self->{EscapeSequence}); |
| 102 |
my $type = shift(@levels); |
| 103 |
if ($type eq $delim->{'HASH'}) |
| 104 |
{ |
| 105 |
if (UNIVERSAL::isa($ptr, 'HASH')) { |
| 106 |
$ptr->{$key} = {} unless exists $ptr->{$key}; |
| 107 |
$ptr = $ptr->{$key}; |
| 108 |
} else { |
| 109 |
$ptr->[$key] = {} unless defined $ptr->[$key]; |
| 110 |
$ptr = $ptr->[$key]; |
| 111 |
} |
| 112 |
} |
| 113 |
elsif ($type eq $delim->{'ARRAY'}) |
| 114 |
{ |
| 115 |
if (UNIVERSAL::isa($ptr, 'HASH')) { |
| 116 |
$ptr->{$key} = [] unless exists $ptr->{$key}; |
| 117 |
$ptr = $ptr->{$key}; |
| 118 |
} else { |
| 119 |
$ptr->[$key] = [] unless defined $ptr->[$key]; |
| 120 |
$ptr = $ptr->[$key]; |
| 121 |
} |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
die "Type '$type' was not recognized. This should not happen."; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (UNIVERSAL::isa($ptr, 'HASH')) { |
| 130 |
$ptr->{$finalkey} = $value; |
| 131 |
} else { |
| 132 |
$ptr->[$finalkey] = $value; |
| 133 |
} |
| 134 |
} |
| 135 |
return \%expanded; |
| 136 |
} |
| 137 |
|
| 138 |
# |
| 139 |
# Private subroutines |
| 140 |
# |
| 141 |
|
| 142 |
sub _flatten |
| 143 |
{ |
| 144 |
my($self, $flatkey, $v, $delim) = @_; |
| 145 |
|
| 146 |
TRACE("flatten: $self - " . ref($v)); |
| 147 |
|
| 148 |
if(UNIVERSAL::isa($v, 'REF')) |
| 149 |
{ |
| 150 |
$v = $self->_follow_refs($v); |
| 151 |
} |
| 152 |
|
| 153 |
if(UNIVERSAL::isa($v, 'HASH')) |
| 154 |
{ |
| 155 |
return $self->_flatten_hash_level($v, $delim, $flatkey); |
| 156 |
} |
| 157 |
elsif(UNIVERSAL::isa($v, 'ARRAY')) |
| 158 |
{ |
| 159 |
return $self->_flatten_array_level($v, $delim, $flatkey); |
| 160 |
} |
| 161 |
elsif(UNIVERSAL::isa($v, 'GLOB')) |
| 162 |
{ |
| 163 |
$v = $self->_flatten_glob_ref($v); |
| 164 |
} |
| 165 |
elsif(UNIVERSAL::isa($v, 'SCALAR')) |
| 166 |
{ |
| 167 |
$v = $self->_flatten_scalar_ref($v); |
| 168 |
} |
| 169 |
return [$flatkey, $v]; |
| 170 |
} |
| 171 |
|
| 172 |
sub _follow_refs |
| 173 |
{ |
| 174 |
my ($self, $rscalar) = @_; |
| 175 |
while (UNIVERSAL::isa($rscalar, 'REF')) |
| 176 |
{ |
| 177 |
if ($self->{RECURSE_CHECK}{_stringify_ref($rscalar)}++) |
| 178 |
{ |
| 179 |
die "Recursive data structure detected. Cannot flatten recursive structures."; |
| 180 |
} |
| 181 |
|
| 182 |
if(defined $self->{OnRefRef}) { |
| 183 |
if(ref $self->{OnRefRef} eq 'CODE') { |
| 184 |
TRACE("Executing coderef"); |
| 185 |
$rscalar = $self->{OnRefRef}->($rscalar); |
| 186 |
next; |
| 187 |
} elsif($self->{OnRefRef} eq 'warn') { |
| 188 |
warn("$rscalar is a ".(ref $rscalar)." and will be followed"); |
| 189 |
} elsif($self->{OnRefRef} eq 'die') { |
| 190 |
die("$rscalar is a ".(ref $rscalar)); |
| 191 |
} |
| 192 |
} |
| 193 |
$rscalar = $$rscalar; |
| 194 |
} |
| 195 |
return $rscalar; |
| 196 |
} |
| 197 |
|
| 198 |
sub _flatten_hash_level |
| 199 |
{ |
| 200 |
my ($self, $hashref, $delim, $prefix) = @_; |
| 201 |
TRACE("_flatten_hash_level called"); |
| 202 |
|
| 203 |
if ($self->{RECURSE_CHECK}{_stringify_ref($hashref)}++) |
| 204 |
{ |
| 205 |
die "Recursive data structure detected at this point in the structure: '$prefix'. Cannot flatten recursive structures."; |
| 206 |
} |
| 207 |
|
| 208 |
my @flat; |
| 209 |
for my $k (keys %$hashref) |
| 210 |
{ |
| 211 |
TRACE("_flatten_hash_level: flattening: $k"); |
| 212 |
my $v = $hashref->{$k}; |
| 213 |
$k = $self->_escape($k, $self->{EscapeSequence}, [values %$delim]); |
| 214 |
my $flatkey = (defined($prefix) ? $prefix.$delim->{'HASH'}.$k : $k); |
| 215 |
push @flat, $self->_flatten($flatkey, $v, $delim); |
| 216 |
} |
| 217 |
return @flat; |
| 218 |
} |
| 219 |
|
| 220 |
sub _flatten_array_level |
| 221 |
{ |
| 222 |
my ($self, $arrayref, $delim, $prefix) = @_; |
| 223 |
|
| 224 |
if ($self->{RECURSE_CHECK}{_stringify_ref($arrayref)}++) |
| 225 |
{ |
| 226 |
die "Recursive data structure detected at this point in the structure: '$prefix'. Cannot flatten recursive structures."; |
| 227 |
} |
| 228 |
|
| 229 |
my @flat; |
| 230 |
foreach my $ind (0 .. $#$arrayref) |
| 231 |
{ |
| 232 |
my $flatkey = (defined($prefix) ? $prefix.$delim->{'ARRAY'}.$ind : $ind); |
| 233 |
my $v = $arrayref->[$ind]; |
| 234 |
push @flat, $self->_flatten($flatkey, $v, $delim); |
| 235 |
} |
| 236 |
return @flat; |
| 237 |
} |
| 238 |
|
| 239 |
sub _flatten_scalar_ref |
| 240 |
{ |
| 241 |
my ($self, $rscalar) = @_; |
| 242 |
if(defined $self->{OnRefScalar}) { |
| 243 |
if(ref $self->{OnRefScalar} eq 'CODE') { |
| 244 |
TRACE("Executing coderef"); |
| 245 |
return $self->{OnRefScalar}->($rscalar); |
| 246 |
} elsif($self->{OnRefScalar} eq 'warn') { |
| 247 |
warn("$rscalar is a ".(ref $rscalar)." and will be followed"); |
| 248 |
} elsif($self->{OnRefScalar} eq 'die') { |
| 249 |
die("$rscalar is a ".(ref $rscalar)); |
| 250 |
} |
| 251 |
} |
| 252 |
return $$rscalar; |
| 253 |
} |
| 254 |
|
| 255 |
sub _flatten_glob_ref |
| 256 |
{ |
| 257 |
my($self, $rglob) = @_; |
| 258 |
if(defined $self->{OnRefGlob}) { |
| 259 |
if(ref $self->{OnRefGlob} eq 'CODE') { |
| 260 |
TRACE("Executing coderef"); |
| 261 |
return $self->{OnRefGlob}->($rglob); |
| 262 |
} elsif($self->{OnRefGlob} eq 'warn') { |
| 263 |
warn("$rglob is a ".(ref $rglob)." and will be followed"); |
| 264 |
} elsif($self->{OnRefGlob} eq 'die') { |
| 265 |
die("$rglob is a ".(ref $rglob)); |
| 266 |
} |
| 267 |
} |
| 268 |
return $rglob; |
| 269 |
} |
| 270 |
|
| 271 |
sub _escape |
| 272 |
{ |
| 273 |
my ($self, $string, $eseq, $delim) = @_; |
| 274 |
return $string unless($eseq); #no-op |
| 275 |
$delim = [] unless(ref $delim eq 'ARRAY'); |
| 276 |
|
| 277 |
foreach my $char($eseq, @$delim) { |
| 278 |
next unless(defined $char && length($char)); |
| 279 |
$string =~ s/\Q$char\E/$eseq$char/sg; |
| 280 |
} |
| 281 |
|
| 282 |
return $string; |
| 283 |
} |
| 284 |
|
| 285 |
sub _unescape |
| 286 |
{ |
| 287 |
my ($self, $string, $eseq) = @_; |
| 288 |
return $string unless($eseq); #no-op |
| 289 |
|
| 290 |
#Remove escape characters apart from double-escapes |
| 291 |
$string =~ s/\Q$eseq\E(?!\Q$eseq\E)//gs; |
| 292 |
|
| 293 |
#Fold double-escapes down to single escapes |
| 294 |
$string =~ s/\Q$eseq$eseq\E/$eseq/gs; |
| 295 |
|
| 296 |
return $string; |
| 297 |
} |
| 298 |
|
| 299 |
sub _stringify_ref { |
| 300 |
my $ref = shift; |
| 301 |
return unless ref($ref); #Undef if not a reference |
| 302 |
return overload::StrVal($ref) if(HAVE_OVERLOAD && overload::Overloaded($ref)); |
| 303 |
return $ref.''; #Force type conversion here |
| 304 |
} |
| 305 |
|
| 306 |
#Log::Trace stubs |
| 307 |
sub TRACE {} |
| 308 |
sub DUMP {} |
| 309 |
|
| 310 |
1; |
| 311 |
|
| 312 |
=head1 NAME |
| 313 |
|
| 314 |
Hash::Flatten - flatten/unflatten complex data hashes |
| 315 |
|
| 316 |
=head1 SYNOPSIS |
| 317 |
|
| 318 |
# Exported functions |
| 319 |
use Hash::Flatten qw(:all); |
| 320 |
$flat_hash = flatten($nested_hash); |
| 321 |
$nested_hash = unflatten($flat_hash); |
| 322 |
|
| 323 |
# OO interface |
| 324 |
my $o = new Hash::Flatten({ |
| 325 |
HashDelimiter => '->', |
| 326 |
ArrayDelimiter => '=>', |
| 327 |
OnRefScalar => 'warn', |
| 328 |
}); |
| 329 |
$flat_hash = $o->flatten($nested_hash); |
| 330 |
$nested_hash = $o->unflatten($flat_hash); |
| 331 |
|
| 332 |
=head1 DESCRIPTION |
| 333 |
|
| 334 |
Converts back and forth between a nested hash structure and a flat hash of delimited key-value pairs. |
| 335 |
Useful for protocols that only support key-value pairs (such as CGI and DBMs). |
| 336 |
|
| 337 |
=head2 Functional interface |
| 338 |
|
| 339 |
=over 4 |
| 340 |
|
| 341 |
=item $flat_hash = flatten($nested_hash, \%options) |
| 342 |
|
| 343 |
Reduces a nested data-structure to key-value form. The top-level container must be hashref. For example: |
| 344 |
|
| 345 |
$nested = { |
| 346 |
'x' => 1, |
| 347 |
'y' => { |
| 348 |
'a' => 2, |
| 349 |
'b' => 3 |
| 350 |
}, |
| 351 |
'z' => [ |
| 352 |
'a', 'b', 'c' |
| 353 |
] |
| 354 |
} |
| 355 |
|
| 356 |
$flat = flatten($nested); |
| 357 |
use Data::Dumper; |
| 358 |
print Dumper($flat); |
| 359 |
|
| 360 |
$VAR1 = { |
| 361 |
'y.a' => 2, |
| 362 |
'x' => 1, |
| 363 |
'y.b' => 3, |
| 364 |
'z:0' => 'a', |
| 365 |
'z:1' => 'b', |
| 366 |
'z:2' => 'c' |
| 367 |
}; |
| 368 |
|
| 369 |
The C<\%options> hashref can be used to override the default behaviour (see L</OPTIONS>). |
| 370 |
|
| 371 |
=item $nested_hash = unflatten($flat_hash, \%options) |
| 372 |
|
| 373 |
The unflatten() routine takes the flattened hash and returns the original nested hash (see L</CAVEATS> though). |
| 374 |
|
| 375 |
=back |
| 376 |
|
| 377 |
=head2 OO interface |
| 378 |
|
| 379 |
=over 4 |
| 380 |
|
| 381 |
=item $o = new Hash::Flatten(\%options) |
| 382 |
|
| 383 |
Options can be squirreled away in an object (see L</OPTIONS>) |
| 384 |
|
| 385 |
=item $flat = $o->flatten($nested) |
| 386 |
|
| 387 |
Flatten the structure using the options stored in the object. |
| 388 |
|
| 389 |
=item $nested = $o->unflatten($flat) |
| 390 |
|
| 391 |
Unflatten the structure using the options stored in the object. |
| 392 |
|
| 393 |
=back |
| 394 |
|
| 395 |
=head1 OPTIONS |
| 396 |
|
| 397 |
=over 4 |
| 398 |
|
| 399 |
=item HashDelimiter and ArrayDelimiter |
| 400 |
|
| 401 |
By default, hash dereferences are denoted by a dot, and array dereferences are denoted by a colon. However |
| 402 |
you may change these characters to any string you want, because you don't want there to be any confusion as to |
| 403 |
which part of a string is the 'key' and which is the 'delimiter'. You may use multicharacter strings |
| 404 |
if you prefer. |
| 405 |
|
| 406 |
=item OnRefScalar and OnRefRef and OnRefGlob |
| 407 |
|
| 408 |
Behaviour if a reference of this type is encountered during flattening. |
| 409 |
Possible values are 'die', 'warn' (default behaviour but warns) or a coderef |
| 410 |
which is passed the reference and should return the flattened value. |
| 411 |
|
| 412 |
By default references to references, and references to scalars, are followed silently. |
| 413 |
|
| 414 |
=item EscapeSequence |
| 415 |
|
| 416 |
This is the character or sequence of characters that will be used to escape the hash and array delimiters. |
| 417 |
The default escape sequence is '\\'. The escaping strategy is to place the escape sequence in front of |
| 418 |
delimiter sequences; the escape sequence itself is escaped by replacing it with two instances. |
| 419 |
|
| 420 |
=item DisableEscapes |
| 421 |
|
| 422 |
Stop the escaping from happening. No escape sequences will be added to flattened output, nor interpreted on the way back. |
| 423 |
|
| 424 |
B<WARNING:> If your structure has keys that contain the delimiter characters, it will not be possible to unflatten the |
| 425 |
structure correctly. |
| 426 |
|
| 427 |
=back |
| 428 |
|
| 429 |
=head1 CAVEATS |
| 430 |
|
| 431 |
Any blessings will be discarded during flattening, so that if you flatten an object you must re-bless() it on unflattening. |
| 432 |
|
| 433 |
Note that there is no delimiter for scalar references, or references to references. |
| 434 |
If your structure to be flattened contains scalar, or reference, references these will be followed by default, i.e. |
| 435 |
C<'foo' =E<gt> \\\\\\$foo> |
| 436 |
will be collapsed to |
| 437 |
C<'foo' =E<gt> $foo>. |
| 438 |
You can override this behaviour using the OnRefScalar and OnRefRef constructor option. |
| 439 |
|
| 440 |
Recursive structures are detected and cause a fatal error. |
| 441 |
|
| 442 |
=head1 SEE ALSO |
| 443 |
|
| 444 |
The perlmonks site has a helpful introduction to when and why you |
| 445 |
might want to flatten a hash: http://www.perlmonks.org/index.pl?node_id=234186 |
| 446 |
|
| 447 |
=over 4 |
| 448 |
|
| 449 |
=item CGI::Expand |
| 450 |
|
| 451 |
Unflattens hashes using "." as a delimiter, similar to Template::Toolkit's behaviour. |
| 452 |
|
| 453 |
=item Tie::MultiDim |
| 454 |
|
| 455 |
This provides a tie interface to unflattening a data structure if you specify a "template" for the structure of the data. |
| 456 |
|
| 457 |
=item MLDBM |
| 458 |
|
| 459 |
This also provides a tie interface but reduces a nested structure to key-value form by serialising the values below the top level. |
| 460 |
|
| 461 |
=back |
| 462 |
|
| 463 |
=head1 VERSION |
| 464 |
|
| 465 |
$Id: Flatten.pm,v 1.19 2009/05/09 12:42:02 jamiel Exp $ |
| 466 |
|
| 467 |
=head1 AUTHOR |
| 468 |
|
| 469 |
John Alden E<amp> P Kent E<lt>cpan _at_ bbc _dot_ co _dot_ ukE<gt> |
| 470 |
|
| 471 |
=head1 COPYRIGHT |
| 472 |
|
| 473 |
(c) BBC 2005. This program is free software; you can redistribute it and/or |
| 474 |
modify it under the GNU GPL. |
| 475 |
|
| 476 |
See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt |
| 477 |
|
| 478 |
=cut |