1 |
############################################## |
## --------------------------------------------------------------------------- |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## --------------------------------------------------------------------------- |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.8 2003/02/09 05:10:56 joko |
6 |
# Revision 1.5 2002/12/16 19:57:54 joko |
## + minor update |
7 |
# + option 'init' |
## |
8 |
# |
## Revision 1.7 2003/01/19 03:26:59 joko |
9 |
# Revision 1.4 2002/12/05 13:56:49 joko |
## + added 'sub deep_copy' - refactored from libp |
10 |
# - var_deref |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
11 |
# + expand - more sophisticated dereferencing with callbacks using Iterate |
## |
12 |
# |
## Revision 1.6 2002/12/23 11:27:53 jonen |
13 |
# Revision 1.3 2002/12/03 05:34:55 joko |
## + changed behavior WATCH! |
14 |
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
## |
15 |
# |
## Revision 1.5 2002/12/16 19:57:54 joko |
16 |
# Revision 1.2 2002/12/01 04:44:07 joko |
## + option 'init' |
17 |
# + code from Data::Transform::OO |
## |
18 |
# |
## Revision 1.4 2002/12/05 13:56:49 joko |
19 |
# Revision 1.1 2002/11/29 04:49:20 joko |
## - var_deref |
20 |
# + initial check-in |
## + expand - more sophisticated dereferencing with callbacks using Iterate |
21 |
# |
## |
22 |
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
## Revision 1.3 2002/12/03 05:34:55 joko |
23 |
# + new |
## + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
24 |
# |
## |
25 |
############################################## |
## Revision 1.2 2002/12/01 04:44:07 joko |
26 |
|
## + code from Data::Transform::OO |
27 |
|
## |
28 |
|
## Revision 1.1 2002/11/29 04:49:20 joko |
29 |
|
## + initial check-in |
30 |
|
## |
31 |
|
## Revision 1.1 2002/10/10 03:26:00 cvsjoko |
32 |
|
## + new |
33 |
|
## --------------------------------------------------------------------------- |
34 |
|
|
35 |
|
|
36 |
package Data::Transform::Deep; |
package Data::Transform::Deep; |
47 |
&hash2object |
&hash2object |
48 |
&refexpr2perlref |
&refexpr2perlref |
49 |
&expand |
&expand |
50 |
|
&deep_copy |
51 |
|
&merge |
52 |
); |
); |
53 |
# &var_deref |
# &var_deref |
54 |
# &getStructSlotByStringyAddress |
# &getStructSlotByStringyAddress |
56 |
use attributes; |
use attributes; |
57 |
use Data::Dumper; |
use Data::Dumper; |
58 |
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
59 |
use libp qw( deep_copy ); |
|
60 |
|
use Hash::Merge qw( merge ); |
61 |
use Iterate; |
use Iterate; |
62 |
|
|
63 |
sub var_NumericalHashToArray { |
sub var_NumericalHashToArray { |
321 |
|
|
322 |
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
323 |
@fields = keys %{$data} if $options->{init}; |
@fields = keys %{$data} if $options->{init}; |
324 |
|
# @fields = keys %{$data} if $options->{mixin}; |
325 |
|
|
326 |
foreach (@fields) { |
foreach (@fields) { |
327 |
push @indexstack, $_; |
my $field = $_; |
328 |
|
push @indexstack, $field; |
329 |
|
|
330 |
# determine type of object |
# determine type of object |
331 |
my $ref = ref $object->{$_}; |
my $ref = ref $object->{$field}; |
332 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
333 |
if ($ref) { |
if ($ref) { |
334 |
hash2object_traverse_mixin($object->{$_}, $data, 1, $options); |
hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
335 |
} else { |
} else { |
336 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
337 |
$object->{$_} = $val; |
|
338 |
|
#print Dumper($options); |
339 |
|
my $field_target = $field; |
340 |
|
if (my $pattern = $options->{pattern_strip_key}) { |
341 |
|
print "pattern: $pattern", "\n"; |
342 |
|
$field_target =~ s/$pattern//; |
343 |
|
print "field: $field_target", "\n"; |
344 |
|
} |
345 |
|
|
346 |
|
$object->{$field_target} = $val if defined $val; |
347 |
} |
} |
348 |
pop @indexstack; |
pop @indexstack; |
349 |
} |
} |
374 |
push @indexstack, $i; |
push @indexstack, $i; |
375 |
my $ref = ref $_; |
my $ref = ref $_; |
376 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
377 |
if ($ref && $_) { |
# if ($ref && $_) { |
378 |
|
if ($ref) { |
379 |
hash2object_traverse_mixin($_, $data, 1, $options); |
hash2object_traverse_mixin($_, $data, 1, $options); |
380 |
} else { |
} else { |
381 |
$object->[$i] = $_; |
$object->[$i] = $_ if defined $_; |
382 |
} |
} |
383 |
pop @indexstack; |
pop @indexstack; |
384 |
$i++; |
$i++; |
473 |
return (join('->', @parts_capsule), \@parts_pure); |
return (join('->', @parts_capsule), \@parts_pure); |
474 |
} |
} |
475 |
|
|
476 |
|
# ACK's go to ... |
477 |
|
sub deep_copy { |
478 |
|
my $this = shift; |
479 |
|
if (not ref $this) { |
480 |
|
$this; |
481 |
|
} elsif (ref $this eq "ARRAY") { |
482 |
|
[map deep_copy($_), @$this]; |
483 |
|
} elsif (ref $this eq "HASH") { |
484 |
|
+{map { $_ => deep_copy($this->{$_}) } keys %$this}; |
485 |
|
} elsif (ref $this eq "CODE") { |
486 |
|
$this; |
487 |
|
#} else { die "deep_copy asks: what type is $this?" } |
488 |
|
} else { print "deep_copy asks: what type is $this?", "\n"; } |
489 |
|
} |
490 |
|
|
491 |
1; |
1; |