1 |
############################################## |
## --------------------------------------------------------------------------- |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## --------------------------------------------------------------------------- |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.12 2003/02/20 22:45:19 joko |
6 |
# Revision 1.6 2002/12/23 11:27:53 jonen |
## fix regarding new deep_copy |
7 |
# + changed behavior WATCH! |
## |
8 |
# |
## Revision 1.11 2003/02/20 21:13:54 joko |
9 |
# Revision 1.5 2002/12/16 19:57:54 joko |
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
10 |
# + option 'init' |
## |
11 |
# |
## Revision 1.10 2003/02/20 20:48:00 joko |
12 |
# Revision 1.4 2002/12/05 13:56:49 joko |
## - refactored lots of code to Data::Code::Ref |
13 |
# - var_deref |
## + alternative 'deep_copy' implementation |
14 |
# + expand - more sophisticated dereferencing with callbacks using Iterate |
## |
15 |
# |
## Revision 1.9 2003/02/18 19:35:56 joko |
16 |
# Revision 1.3 2002/12/03 05:34:55 joko |
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
17 |
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
## |
18 |
# |
## Revision 1.8 2003/02/09 05:10:56 joko |
19 |
# Revision 1.2 2002/12/01 04:44:07 joko |
## + minor update |
20 |
# + code from Data::Transform::OO |
## |
21 |
# |
## Revision 1.7 2003/01/19 03:26:59 joko |
22 |
# Revision 1.1 2002/11/29 04:49:20 joko |
## + added 'sub deep_copy' - refactored from libp |
23 |
# + initial check-in |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
24 |
# |
## |
25 |
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
## Revision 1.6 2002/12/23 11:27:53 jonen |
26 |
# + new |
## + changed behavior WATCH! |
27 |
# |
## |
28 |
############################################## |
## Revision 1.5 2002/12/16 19:57:54 joko |
29 |
|
## + option 'init' |
30 |
|
## |
31 |
|
## Revision 1.4 2002/12/05 13:56:49 joko |
32 |
|
## - var_deref |
33 |
|
## + expand - more sophisticated dereferencing with callbacks using Iterate |
34 |
|
## |
35 |
|
## Revision 1.3 2002/12/03 05:34:55 joko |
36 |
|
## + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
37 |
|
## |
38 |
|
## Revision 1.2 2002/12/01 04:44:07 joko |
39 |
|
## + code from Data::Transform::OO |
40 |
|
## |
41 |
|
## Revision 1.1 2002/11/29 04:49:20 joko |
42 |
|
## + initial check-in |
43 |
|
## |
44 |
|
## Revision 1.1 2002/10/10 03:26:00 cvsjoko |
45 |
|
## + new |
46 |
|
## --------------------------------------------------------------------------- |
47 |
|
|
48 |
|
|
49 |
package Data::Transform::Deep; |
package Data::Transform::Deep; |
54 |
require Exporter; |
require Exporter; |
55 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
56 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
|
&var_NumericalHashToArray |
|
|
&var_mixin |
|
|
&object2hash |
|
|
&hash2object |
|
|
&refexpr2perlref |
|
57 |
&expand |
&expand |
58 |
|
&deep_copy |
59 |
|
&merge_to |
60 |
); |
); |
61 |
# &var_deref |
|
|
# &getStructSlotByStringyAddress |
|
62 |
|
|
63 |
use attributes; |
use attributes; |
64 |
use Data::Dumper; |
use Data::Dumper; |
|
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
|
|
use libp qw( deep_copy ); |
|
65 |
use Iterate; |
use Iterate; |
66 |
|
|
67 |
sub var_NumericalHashToArray { |
use Pitonyak::DeepCopy; |
68 |
|
use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar ); |
69 |
|
use Data::Code::Ref qw( ref_slot ); |
70 |
|
|
71 |
|
sub numhash2list { |
72 |
my $vref = shift; |
my $vref = shift; |
73 |
#my $context = shift; |
#my $context = shift; |
74 |
if (ref $vref eq 'HASH') { |
if (ref $vref eq 'HASH') { |
84 |
} |
} |
85 |
$vref->{$_} = \@arr; |
$vref->{$_} = \@arr; |
86 |
} |
} |
87 |
var_NumericalHashToArray($vref->{$_}); |
numhash2list($vref->{$_}); |
88 |
} |
} |
89 |
# } else { |
# } else { |
90 |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
94 |
} |
} |
95 |
|
|
96 |
|
|
97 |
sub var_deref { |
# FIXME: could this be refactored using expand? |
98 |
|
# btw: "expand": look at scripts@CPAN (System Administration): |
99 |
|
# there is a perl make with perl |
100 |
|
sub _var_deref_test { |
101 |
my $obj = shift; |
my $obj = shift; |
102 |
my $options = shift; |
my $options = shift; |
103 |
my $result; |
my $result; |
142 |
my $options = shift; |
my $options = shift; |
143 |
my $result; |
my $result; |
144 |
|
|
145 |
if ((ref $obj) eq 'ARRAY') { |
#print "ref: ", ref $obj, "\n"; |
146 |
|
|
147 |
|
if (ref $obj eq 'ARRAY') { |
148 |
|
|
149 |
IterArray @$obj, sub { |
IterArray @$obj, sub { |
150 |
my $item; |
my $item; |
160 |
#$item = deep_copy($_[0]); |
#$item = deep_copy($_[0]); |
161 |
$item = $_[0]; |
$item = $_[0]; |
162 |
# conversions/encodings |
# conversions/encodings |
163 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
164 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
165 |
} |
} |
166 |
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
167 |
push(@{$result}, $item); # use item in any case |
push(@{$result}, $item); # use item in any case |
168 |
|
|
169 |
} |
} |
170 |
|
|
171 |
|
} elsif (ref $obj eq 'CODE') { |
172 |
|
#print Dumper($obj); |
173 |
|
#exit; |
174 |
|
|
175 |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
176 |
} else { |
} elsif (ref $obj) { |
177 |
|
|
178 |
IterHash %$obj, sub { |
IterHash %$obj, sub { |
179 |
my $item; |
my $item; |
190 |
#$item = deep_copy($_[1]); |
#$item = deep_copy($_[1]); |
191 |
$item = $_[1]; |
$item = $_[1]; |
192 |
# conversions/encodings |
# conversions/encodings |
193 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
194 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
195 |
} |
} |
196 |
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
197 |
$result->{$_[0]} = $item; # use item in any case |
$result->{$_[0]} = $item; # use item in any case |
198 |
} |
} |
199 |
|
|
200 |
|
} else { |
201 |
|
#die ("not a reference!"); |
202 |
|
$result = $obj; |
203 |
|
|
204 |
} |
} |
205 |
|
|
206 |
# convert all values to utf8 (inside complex struct) |
# convert all values to utf8 (inside complex struct) |
218 |
|
|
219 |
|
|
220 |
my @indexstack; |
my @indexstack; |
221 |
sub var_traverse_mixin_update { |
sub _var_traverse_mixin_update_old { |
222 |
my $obj = shift; |
my $obj = shift; |
223 |
my $data = shift; |
my $data = shift; |
224 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
262 |
#return $result; |
#return $result; |
263 |
} |
} |
264 |
|
|
265 |
sub object2hash { |
sub merge_to { |
266 |
my $object = shift; |
_hash2object(@_); |
267 |
my $options = shift; |
# TODO: |
268 |
#my $deref = var_deref($object); |
# re-implement using CPAN's "Iterate". |
|
my $deref = expand($object, $options); |
|
|
#var2utf8($deref) if ($options->{utf8}); |
|
|
return $deref; |
|
269 |
} |
} |
270 |
|
|
271 |
|
|
272 |
# todo: maybe do diff(s) here sometimes!? |
# todo: maybe do diff(s) here sometimes!? |
273 |
sub hash2object { |
sub _hash2object { |
274 |
my $object = shift; |
my $object = shift; |
275 |
my $data = shift; |
my $data = shift; |
276 |
my $options = shift; |
my $options = shift; |
279 |
|
|
280 |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
281 |
# this method corrects this |
# this method corrects this |
282 |
var_NumericalHashToArray($data) if ($options->{php}); |
numhash2list($data) if ($options->{php}); |
283 |
|
|
284 |
# utf8-conversion/-encoding (essential for I18N) |
# utf8-conversion/-encoding (essential for I18N) |
285 |
var_utf2iso($data) if ($options->{utf8}); |
var_utf2iso($data) if ($options->{utf8}); |
291 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
292 |
|
|
293 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
294 |
hash2object_traverse_mixin($object, $data, 0, $options); |
_hash2object_traverse_mixin($object, $data, 0, $options); |
295 |
|
|
296 |
# done in core mixin function? |
# done in core mixin function? |
297 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
314 |
#my @indexstack; |
#my @indexstack; |
315 |
|
|
316 |
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
317 |
sub hash2object_traverse_mixin { |
sub _hash2object_traverse_mixin { |
318 |
my $object = shift; |
my $object = shift; |
319 |
my $data = shift; |
my $data = shift; |
320 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
332 |
# what's exactly done here? hmmm.... it works ;) |
# what's exactly done here? hmmm.... it works ;) |
333 |
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
334 |
# extend! check! |
# extend! check! |
335 |
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
|
336 |
|
#print attributes::reftype($object), "\n"; |
337 |
|
|
338 |
|
# V1 |
339 |
|
#if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
340 |
|
# V2 |
341 |
|
my $reftype = attributes::reftype($object); |
342 |
|
#print "reftype: '$reftype'", "\n"; |
343 |
|
if ($reftype eq 'HASH') { |
344 |
|
|
345 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
346 |
|
|
353 |
# @fields = keys %{$data} if $options->{mixin}; |
# @fields = keys %{$data} if $options->{mixin}; |
354 |
|
|
355 |
foreach (@fields) { |
foreach (@fields) { |
356 |
push @indexstack, $_; |
my $field = $_; |
357 |
|
push @indexstack, $field; |
358 |
|
|
359 |
# determine type of object |
# determine type of object |
360 |
my $ref = ref $object->{$_}; |
my $ref = ref $object->{$field}; |
361 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
362 |
if ($ref) { |
if ($ref) { |
363 |
hash2object_traverse_mixin($object->{$_}, $data, 1, $options); |
_hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
364 |
} else { |
} else { |
365 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = ref_slot($data, \@indexstack); |
366 |
$object->{$_} = $val if defined $val; |
|
367 |
|
#print Dumper($options); |
368 |
|
my $field_target = $field; |
369 |
|
if (my $pattern = $options->{pattern_strip_key}) { |
370 |
|
print "pattern: $pattern", "\n"; |
371 |
|
$field_target =~ s/$pattern//; |
372 |
|
print "field: $field_target", "\n"; |
373 |
|
} |
374 |
|
|
375 |
|
$object->{$field_target} = $val if defined $val; |
376 |
} |
} |
377 |
pop @indexstack; |
pop @indexstack; |
378 |
} |
} |
405 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
406 |
# if ($ref && $_) { |
# if ($ref && $_) { |
407 |
if ($ref) { |
if ($ref) { |
408 |
hash2object_traverse_mixin($_, $data, 1, $options); |
_hash2object_traverse_mixin($_, $data, 1, $options); |
409 |
} else { |
} else { |
410 |
$object->[$i] = $_ if defined $_; |
$object->[$i] = $_ if defined $_; |
411 |
} |
} |
412 |
pop @indexstack; |
pop @indexstack; |
413 |
$i++; |
$i++; |
414 |
} |
} |
415 |
|
|
416 |
|
=pod |
417 |
|
# object? |
418 |
|
} else { |
419 |
|
print "reference: ", ref $object, "\n"; |
420 |
|
print "reftype: ", $reftype, "\n"; |
421 |
|
die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!"); |
422 |
|
=cut |
423 |
|
|
424 |
} |
} |
425 |
|
|
426 |
} |
} |
427 |
|
|
428 |
|
|
429 |
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
# ACK's go to Randal L. Schwartz <merlyn@stonehenge.com> |
430 |
# TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ? ;-) |
# on the website: |
431 |
sub getStructSlotByStringyAddress { |
# This text is copyright by Miller-Freeman, Inc., and is used with their permission. |
432 |
my $var = shift; |
# Further distribution or use is not permitted. |
433 |
my $indexstack_ref = shift; |
# please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html |
434 |
my @indexstack = @{$indexstack_ref}; |
sub deep_copy1 { |
435 |
my $joiner = '->'; |
my $this = shift; |
436 |
my @evlist; |
if (not ref $this) { |
437 |
foreach (@indexstack) { |
$this; |
438 |
my $elem; |
} elsif (ref $this eq "ARRAY") { |
439 |
if ($_ =~ m/\d+/) { $elem = "[$_]"; } |
[map deep_copy1($_), @$this]; |
440 |
if ($_ =~ m/\D+/) { $elem = "{$_}"; } |
} elsif (ref $this eq "HASH") { |
441 |
push @evlist, $elem; |
+{map { $_ => deep_copy1($this->{$_}) } keys %$this}; |
442 |
} |
} elsif (ref $this eq "CODE") { |
443 |
my $evstring = join($joiner, @evlist); |
$this; |
444 |
$evstring = 'return $var->' . $evstring . ';'; |
} else { die "deep_copy1 asks: what type is $this?" } |
445 |
#print "ev: $evstring\n"; |
#} else { print "deep_copy asks: what type is $this?", "\n"; } |
|
return eval($evstring); |
|
446 |
} |
} |
447 |
|
|
448 |
|
# ACK's go to Andrew Pitonyak |
449 |
sub refexpr2perlref { |
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
450 |
|
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
451 |
my $obj = shift; |
sub deep_copy { |
452 |
my $expr = shift; |
Pitonyak::DeepCopy::deep_copy(@_); |
|
my $callbackMap = shift; |
|
|
|
|
|
my $value; |
|
|
# detect for callback (code-reference) |
|
|
if (ref($expr) eq 'CODE') { |
|
|
$value = &$expr($obj); |
|
|
|
|
|
} elsif ($expr =~ m/->/) { |
|
|
# use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute) |
|
|
(my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr); |
|
|
#print "\n", "expr: $expr"; |
|
|
#print "\n", "objPerlRefString: $objPerlRefString"; |
|
|
$value = eval('$obj' . '->' . $objPerlRefString); |
|
|
|
|
|
# if value isn't set, try to "fallback" to callbackMap |
|
|
# callbacks are applied this way: |
|
|
# take the last element of the expression parts and check if this string exists as a key in the callback-map |
|
|
if (!$value && $callbackMap) { |
|
|
#print Dumper($callbackMap); |
|
|
|
|
|
# prepare needle |
|
|
my @parts = @$parts; |
|
|
my $count = $#parts; |
|
|
my $needle = $parts[$count]; |
|
|
|
|
|
# prepare haystack |
|
|
my @haystack = keys %$callbackMap; |
|
|
if (grep($needle, @haystack)) { |
|
|
$value = $callbackMap->{$needle}->(); |
|
|
#print "value: ", $value, "\n"; |
|
|
} |
|
|
} |
|
|
|
|
|
} else { |
|
|
# use expr as simple scalar key (attributename) |
|
|
$value = $obj->{$expr}; |
|
|
} |
|
|
|
|
|
return $value; |
|
|
|
|
453 |
} |
} |
454 |
|
|
455 |
|
|
|
sub refexpr2perlref_parts { |
|
|
my $expr = shift; |
|
|
|
|
|
# split expression by dereference operators first |
|
|
my @parts_pure = split(/->/, $expr); |
|
|
|
|
|
# wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index), |
|
|
# use {}'s, if there are word-characters in it (=> alphanumeric = hash-key) |
|
|
my @parts_capsule = @parts_pure; |
|
|
map { |
|
|
m/^\d+$/ && ($_ = "[$_]") || ($_ = "{$_}"); |
|
|
} @parts_capsule; |
|
|
|
|
|
# join parts with dereference operators together again and return built string |
|
|
return (join('->', @parts_capsule), \@parts_pure); |
|
|
} |
|
|
|
|
456 |
1; |
1; |
457 |
|
__END__ |