2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.14 2003/02/22 17:13:55 jonen |
6 |
|
## + added function 'childObject2string()' to encode 'child'-references to option related string |
7 |
|
## + use new option at 'expand()' for replacing 'childObject2string' |
8 |
|
## |
9 |
|
## Revision 1.13 2003/02/21 01:48:50 joko |
10 |
|
## renamed core function |
11 |
|
## |
12 |
|
## Revision 1.12 2003/02/20 22:45:19 joko |
13 |
|
## fix regarding new deep_copy |
14 |
|
## |
15 |
|
## Revision 1.11 2003/02/20 21:13:54 joko |
16 |
|
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
17 |
|
## |
18 |
|
## Revision 1.10 2003/02/20 20:48:00 joko |
19 |
|
## - refactored lots of code to Data::Code::Ref |
20 |
|
## + alternative 'deep_copy' implementation |
21 |
|
## |
22 |
|
## Revision 1.9 2003/02/18 19:35:56 joko |
23 |
|
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
24 |
|
## |
25 |
|
## Revision 1.8 2003/02/09 05:10:56 joko |
26 |
|
## + minor update |
27 |
|
## |
28 |
## Revision 1.7 2003/01/19 03:26:59 joko |
## Revision 1.7 2003/01/19 03:26:59 joko |
29 |
## + added 'sub deep_copy' - refactored from libp |
## + added 'sub deep_copy' - refactored from libp |
30 |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
61 |
require Exporter; |
require Exporter; |
62 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
63 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
|
&var_NumericalHashToArray |
|
|
&var_mixin |
|
|
&object2hash |
|
|
&hash2object |
|
|
&refexpr2perlref |
|
64 |
&expand |
&expand |
65 |
&deep_copy |
&deep_copy |
66 |
&merge |
&merge_to |
67 |
); |
); |
68 |
# &var_deref |
|
|
# &getStructSlotByStringyAddress |
|
69 |
|
|
70 |
use attributes; |
use attributes; |
71 |
use Data::Dumper; |
use Data::Dumper; |
|
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
|
|
|
|
|
use Hash::Merge qw( merge ); |
|
|
#use libp qw( deep_copy ); |
|
72 |
use Iterate; |
use Iterate; |
73 |
|
|
74 |
sub var_NumericalHashToArray { |
use Pitonyak::DeepCopy; |
75 |
|
use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar ); |
76 |
|
use Data::Code::Ref qw( ref_slot ); |
77 |
|
|
78 |
|
sub numhash2list { |
79 |
my $vref = shift; |
my $vref = shift; |
80 |
#my $context = shift; |
#my $context = shift; |
81 |
if (ref $vref eq 'HASH') { |
if (ref $vref eq 'HASH') { |
91 |
} |
} |
92 |
$vref->{$_} = \@arr; |
$vref->{$_} = \@arr; |
93 |
} |
} |
94 |
var_NumericalHashToArray($vref->{$_}); |
numhash2list($vref->{$_}); |
95 |
} |
} |
96 |
# } else { |
# } else { |
97 |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
101 |
} |
} |
102 |
|
|
103 |
|
|
104 |
sub var_deref { |
# FIXME: could this be refactored using expand? |
105 |
|
# btw: "expand": look at scripts@CPAN (System Administration): |
106 |
|
# there is a perl make with perl |
107 |
|
sub _var_deref_test { |
108 |
my $obj = shift; |
my $obj = shift; |
109 |
my $options = shift; |
my $options = shift; |
110 |
my $result; |
my $result; |
143 |
} |
} |
144 |
|
|
145 |
|
|
146 |
|
# convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML |
147 |
|
# but still using the known latin locale stuff |
148 |
sub expand { |
sub expand { |
149 |
|
|
150 |
my $obj = shift; |
my $obj = shift; |
151 |
my $options = shift; |
my $options = shift; |
152 |
my $result; |
my $result; |
153 |
|
|
154 |
if ((ref $obj) eq 'ARRAY') { |
#print "ref: ", ref $obj, "\n"; |
155 |
|
|
156 |
|
if (ref $obj eq 'ARRAY') { |
157 |
|
|
158 |
IterArray @$obj, sub { |
IterArray @$obj, sub { |
159 |
my $item; |
my $item; |
160 |
# if current item is a reference ... |
# if current item is a reference ... |
161 |
if (ref $_[0]) { |
if (ref $_[0]) { |
162 |
# ... expand structure recursively |
$item = $_[0]; |
163 |
$item = expand($_[0], $options); |
# if $options->{childObj2string} is TRUE or STRING don't expand referenced object, |
164 |
|
# instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> ) |
165 |
|
if ($item && $options->{childObj2string}) { |
166 |
|
$item = childObj2string($item, $options->{childObj2string}); |
167 |
|
} else { |
168 |
|
# ... expand structure recursively |
169 |
|
$item = expand($_[0], $options); |
170 |
|
} |
171 |
# destroy item via seperate callback method (a POST) if requested |
# destroy item via seperate callback method (a POST) if requested |
172 |
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
173 |
|
|
176 |
#$item = deep_copy($_[0]); |
#$item = deep_copy($_[0]); |
177 |
$item = $_[0]; |
$item = $_[0]; |
178 |
# conversions/encodings |
# conversions/encodings |
179 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
180 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
181 |
} |
} |
182 |
#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) |
183 |
push(@{$result}, $item); # use item in any case |
push(@{$result}, $item); # use item in any case |
184 |
|
|
185 |
} |
} |
186 |
|
|
187 |
|
} elsif (ref $obj eq 'CODE') { |
188 |
|
#print Dumper($obj); |
189 |
|
#exit; |
190 |
|
|
191 |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
192 |
} else { |
} elsif (ref $obj) { |
193 |
|
|
194 |
IterHash %$obj, sub { |
IterHash %$obj, sub { |
195 |
my $item; |
my $item; |
196 |
|
|
197 |
# if current item is a reference ... |
# if current item is a reference ... |
198 |
if (ref $_[1]) { |
if (ref $_[1]) { |
199 |
# ... expand structure recursively |
$item = $_[1]; |
200 |
$item = expand($_[1], $options); |
# if $options->{childObj2string} is TRUE or STRING don't expand referenced object, |
201 |
|
# instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> ) |
202 |
|
if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH")) { |
203 |
|
$item = childObj2string($item, $options->{childObj2string}); |
204 |
|
} else { |
205 |
|
# ... expand structure recursively |
206 |
|
$item = expand($_[1], $options); |
207 |
|
} |
208 |
# destroy item via seperate callback method (a POST) if requested |
# destroy item via seperate callback method (a POST) if requested |
209 |
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
210 |
|
|
213 |
#$item = deep_copy($_[1]); |
#$item = deep_copy($_[1]); |
214 |
$item = $_[1]; |
$item = $_[1]; |
215 |
# conversions/encodings |
# conversions/encodings |
216 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
217 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
218 |
} |
} |
219 |
#$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) |
220 |
$result->{$_[0]} = $item; # use item in any case |
$result->{$_[0]} = $item; # use item in any case |
221 |
} |
} |
222 |
|
|
223 |
|
} else { |
224 |
|
#die ("not a reference!"); |
225 |
|
$result = $obj; |
226 |
|
|
227 |
} |
} |
228 |
|
|
229 |
# convert all values to utf8 (inside complex struct) |
# convert all values to utf8 (inside complex struct) |
230 |
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
231 |
# var2utf8($result) if ($options->{utf8}); |
# latin_to_utf8($result) if ($options->{utf8}); |
232 |
|
|
233 |
# destroy persistent object from memory to be sure to get a fresh one next time |
# destroy persistent object from memory to be sure to get a fresh one next time |
234 |
#undef $obj if $options->{destroy}; |
#undef $obj if $options->{destroy}; |
241 |
|
|
242 |
|
|
243 |
my @indexstack; |
my @indexstack; |
244 |
sub var_traverse_mixin_update { |
sub _var_traverse_mixin_update_old { |
245 |
my $obj = shift; |
my $obj = shift; |
246 |
my $data = shift; |
my $data = shift; |
247 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
285 |
#return $result; |
#return $result; |
286 |
} |
} |
287 |
|
|
288 |
sub object2hash { |
sub merge_to { |
289 |
my $object = shift; |
_hash2object(@_); |
290 |
my $options = shift; |
# TODO: |
291 |
#my $deref = var_deref($object); |
# re-implement using CPAN's "Iterate". |
|
my $deref = expand($object, $options); |
|
|
#var2utf8($deref) if ($options->{utf8}); |
|
|
return $deref; |
|
292 |
} |
} |
293 |
|
|
294 |
|
|
295 |
# todo: maybe do diff(s) here sometimes!? |
# todo: maybe do diff(s) here sometimes!? |
296 |
sub hash2object { |
sub _hash2object { |
297 |
my $object = shift; |
my $object = shift; |
298 |
my $data = shift; |
my $data = shift; |
299 |
my $options = shift; |
my $options = shift; |
302 |
|
|
303 |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
304 |
# this method corrects this |
# this method corrects this |
305 |
var_NumericalHashToArray($data) if ($options->{php}); |
numhash2list($data) if ($options->{php}); |
306 |
|
|
307 |
# utf8-conversion/-encoding (essential for I18N) |
# utf8-conversion/-encoding (essential for I18N) |
308 |
var_utf2iso($data) if ($options->{utf8}); |
var_utf2iso($data) if ($options->{utf8}); |
314 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
315 |
|
|
316 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
317 |
hash2object_traverse_mixin($object, $data, 0, $options); |
_hash2object_traverse_mixin($object, $data, 0, $options); |
318 |
|
|
319 |
# done in core mixin function? |
# done in core mixin function? |
320 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
337 |
#my @indexstack; |
#my @indexstack; |
338 |
|
|
339 |
# 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 |
340 |
sub hash2object_traverse_mixin { |
sub _hash2object_traverse_mixin { |
341 |
my $object = shift; |
my $object = shift; |
342 |
my $data = shift; |
my $data = shift; |
343 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
355 |
# what's exactly done here? hmmm.... it works ;) |
# what's exactly done here? hmmm.... it works ;) |
356 |
# 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!!! |
357 |
# extend! check! |
# extend! check! |
358 |
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
|
359 |
|
#print attributes::reftype($object), "\n"; |
360 |
|
|
361 |
|
# V1 |
362 |
|
#if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
363 |
|
# V2 |
364 |
|
my $reftype = attributes::reftype($object); |
365 |
|
#print "reftype: '$reftype'", "\n"; |
366 |
|
if ($reftype eq 'HASH') { |
367 |
|
|
368 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
369 |
|
|
383 |
my $ref = ref $object->{$field}; |
my $ref = ref $object->{$field}; |
384 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
385 |
if ($ref) { |
if ($ref) { |
386 |
hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
_hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
387 |
} else { |
} else { |
388 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = ref_slot($data, \@indexstack); |
389 |
|
|
390 |
print Dumper($options); |
#print Dumper($options); |
391 |
my $field_target = $field; |
my $field_target = $field; |
392 |
if (my $pattern = $options->{pattern_strip_key}) { |
if (my $pattern = $options->{pattern_strip_key}) { |
393 |
print "pattern: $pattern", "\n"; |
print "pattern: $pattern", "\n"; |
428 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
429 |
# if ($ref && $_) { |
# if ($ref && $_) { |
430 |
if ($ref) { |
if ($ref) { |
431 |
hash2object_traverse_mixin($_, $data, 1, $options); |
_hash2object_traverse_mixin($_, $data, 1, $options); |
432 |
} else { |
} else { |
433 |
$object->[$i] = $_ if defined $_; |
$object->[$i] = $_ if defined $_; |
434 |
} |
} |
435 |
pop @indexstack; |
pop @indexstack; |
436 |
$i++; |
$i++; |
437 |
} |
} |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
|
|
# TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ? ;-) |
|
|
sub getStructSlotByStringyAddress { |
|
|
my $var = shift; |
|
|
my $indexstack_ref = shift; |
|
|
my @indexstack = @{$indexstack_ref}; |
|
|
my $joiner = '->'; |
|
|
my @evlist; |
|
|
foreach (@indexstack) { |
|
|
my $elem; |
|
|
if ($_ =~ m/\d+/) { $elem = "[$_]"; } |
|
|
if ($_ =~ m/\D+/) { $elem = "{$_}"; } |
|
|
push @evlist, $elem; |
|
|
} |
|
|
my $evstring = join($joiner, @evlist); |
|
|
$evstring = 'return $var->' . $evstring . ';'; |
|
|
#print "ev: $evstring\n"; |
|
|
return eval($evstring); |
|
|
} |
|
|
|
|
|
|
|
|
sub refexpr2perlref { |
|
|
|
|
|
my $obj = shift; |
|
|
my $expr = shift; |
|
|
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"; |
|
|
} |
|
|
} |
|
438 |
|
|
439 |
|
=pod |
440 |
|
# object? |
441 |
} else { |
} else { |
442 |
# use expr as simple scalar key (attributename) |
print "reference: ", ref $object, "\n"; |
443 |
$value = $obj->{$expr}; |
print "reftype: ", $reftype, "\n"; |
444 |
} |
die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!"); |
445 |
|
=cut |
|
return $value; |
|
446 |
|
|
447 |
} |
} |
|
|
|
|
|
|
|
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; |
|
448 |
|
|
|
# join parts with dereference operators together again and return built string |
|
|
return (join('->', @parts_capsule), \@parts_pure); |
|
449 |
} |
} |
450 |
|
|
451 |
# ACK's go to ... |
|
452 |
sub deep_copy { |
# ACK's go to Randal L. Schwartz <merlyn@stonehenge.com> |
453 |
|
# on the website: |
454 |
|
# This text is copyright by Miller-Freeman, Inc., and is used with their permission. |
455 |
|
# Further distribution or use is not permitted. |
456 |
|
# please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html |
457 |
|
sub deep_copy1 { |
458 |
my $this = shift; |
my $this = shift; |
459 |
if (not ref $this) { |
if (not ref $this) { |
460 |
$this; |
$this; |
461 |
} elsif (ref $this eq "ARRAY") { |
} elsif (ref $this eq "ARRAY") { |
462 |
[map deep_copy($_), @$this]; |
[map deep_copy1($_), @$this]; |
463 |
} elsif (ref $this eq "HASH") { |
} elsif (ref $this eq "HASH") { |
464 |
+{map { $_ => deep_copy($this->{$_}) } keys %$this}; |
+{map { $_ => deep_copy1($this->{$_}) } keys %$this}; |
465 |
} elsif (ref $this eq "CODE") { |
} elsif (ref $this eq "CODE") { |
466 |
$this; |
$this; |
467 |
#} else { die "deep_copy asks: what type is $this?" } |
} else { die "deep_copy1 asks: what type is $this?" } |
468 |
} else { print "deep_copy asks: what type is $this?", "\n"; } |
#} else { print "deep_copy asks: what type is $this?", "\n"; } |
469 |
|
} |
470 |
|
|
471 |
|
# ACK's go to Andrew Pitonyak |
472 |
|
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
473 |
|
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
474 |
|
sub deep_copy { |
475 |
|
Pitonyak::DeepCopy::deep_copy(@_); |
476 |
} |
} |
477 |
|
|
478 |
|
|
479 |
|
sub childObj2string { |
480 |
|
my $obj = shift; |
481 |
|
my $option = shift; |
482 |
|
my $classname = ref $obj; |
483 |
|
|
484 |
|
if($option == 1) { |
485 |
|
my $string = "o_" . $classname . "_" .$obj->{guid}; |
486 |
|
return $string; |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
1; |
1; |
492 |
|
__END__ |