1 |
############################################## |
## --------------------------------------------------------------------------- |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## --------------------------------------------------------------------------- |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.12 2003/02/20 22:45:19 joko |
6 |
# Revision 1.1 2002/11/29 04:49:20 joko |
## fix regarding new deep_copy |
7 |
# + initial check-in |
## |
8 |
# |
## Revision 1.11 2003/02/20 21:13:54 joko |
9 |
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
10 |
# + new |
## |
11 |
# |
## Revision 1.10 2003/02/20 20:48:00 joko |
12 |
############################################## |
## - refactored lots of code to Data::Code::Ref |
13 |
|
## + alternative 'deep_copy' implementation |
14 |
|
## |
15 |
|
## Revision 1.9 2003/02/18 19:35:56 joko |
16 |
|
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
17 |
|
## |
18 |
|
## Revision 1.8 2003/02/09 05:10:56 joko |
19 |
|
## + minor update |
20 |
|
## |
21 |
|
## Revision 1.7 2003/01/19 03:26:59 joko |
22 |
|
## + added 'sub deep_copy' - refactored from libp |
23 |
|
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
24 |
|
## |
25 |
|
## Revision 1.6 2002/12/23 11:27:53 jonen |
26 |
|
## + 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; |
53 |
|
|
54 |
require Exporter; |
require Exporter; |
55 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
56 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
57 |
&var_NumericalHashToArray |
&expand |
58 |
&var_deref |
&deep_copy |
59 |
&var_mixin |
&merge_to |
|
&getStructSlotByStringyAddress |
|
60 |
); |
); |
61 |
|
|
62 |
|
|
63 |
use attributes; |
use attributes; |
64 |
|
use Data::Dumper; |
65 |
|
use Iterate; |
66 |
|
|
67 |
|
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 var_NumericalHashToArray { |
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; |
103 |
my $result; |
my $result; |
104 |
if ((ref $obj) eq 'ARRAY') { |
if ((ref $obj) eq 'ARRAY') { |
105 |
foreach (@{$obj}) { |
foreach (@{$obj}) { |
106 |
my $ref = ref $_; |
my $ref = ref $_; |
107 |
if ($ref) { |
if ($ref) { |
108 |
push(@{$result}, var_deref($_)); |
push(@{$result}, var_deref($_, $options)); |
109 |
|
#undef $_; |
110 |
} else { |
} else { |
111 |
push(@{$result}, $_); |
#push(@{$result}, $_); |
112 |
|
push(@{$result}, deep_copy($_)); |
113 |
} |
} |
114 |
|
#undef $_ if $options->{destroy}; |
115 |
|
#$options->{destroy}->($_) if $options->{destroy}; |
116 |
} |
} |
117 |
|
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
118 |
} else { |
} else { |
119 |
foreach (keys %{$obj}) { |
foreach (keys %{$obj}) { |
120 |
my $key = $_; |
my $key = $_; |
121 |
my $ref = ref $obj->{$_}; |
my $ref = ref $obj->{$_}; |
122 |
if ($ref) { |
if ($ref) { |
123 |
$result->{$_} = var_deref($obj->{$_}); |
$result->{$_} = var_deref($obj->{$_}, $options); |
124 |
|
#undef $obj->{$_}; |
125 |
} else { |
} else { |
126 |
$result->{$_} = $obj->{$_}; |
#$result->{$_} = $obj->{$_}; |
127 |
|
$result->{$_} = deep_copy($obj->{$_}); |
128 |
} |
} |
129 |
|
#undef $obj->{$_} if $options->{destroy}; |
130 |
|
#$options->{destroy}->($obj->{$_}) if $options->{destroy}; |
131 |
} |
} |
132 |
} |
} |
133 |
|
#undef $obj if $options->{destroy}; |
134 |
|
$options->{cb_destroy}->($obj) if $options->{cb_destroy}; |
135 |
return $result; |
return $result; |
136 |
} |
} |
137 |
|
|
138 |
|
|
139 |
|
sub expand { |
140 |
|
|
141 |
|
my $obj = shift; |
142 |
|
my $options = shift; |
143 |
|
my $result; |
144 |
|
|
145 |
|
#print "ref: ", ref $obj, "\n"; |
146 |
|
|
147 |
|
if (ref $obj eq 'ARRAY') { |
148 |
|
|
149 |
|
IterArray @$obj, sub { |
150 |
|
my $item; |
151 |
|
# if current item is a reference ... |
152 |
|
if (ref $_[0]) { |
153 |
|
# ... expand structure recursively |
154 |
|
$item = expand($_[0], $options); |
155 |
|
# destroy item via seperate callback method (a POST) if requested |
156 |
|
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
157 |
|
|
158 |
|
# ... assume plain scalar |
159 |
|
} else { |
160 |
|
#$item = deep_copy($_[0]); |
161 |
|
$item = $_[0]; |
162 |
|
# conversions/encodings |
163 |
|
$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) |
167 |
|
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'; }" ??? |
176 |
|
} elsif (ref $obj) { |
177 |
|
|
178 |
|
IterHash %$obj, sub { |
179 |
|
my $item; |
180 |
|
|
181 |
|
# if current item is a reference ... |
182 |
|
if (ref $_[1]) { |
183 |
|
# ... expand structure recursively |
184 |
|
$item = expand($_[1], $options); |
185 |
|
# destroy item via seperate callback method (a POST) if requested |
186 |
|
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
187 |
|
|
188 |
|
# ... assume plain scalar |
189 |
|
} else { |
190 |
|
#$item = deep_copy($_[1]); |
191 |
|
$item = $_[1]; |
192 |
|
# conversions/encodings |
193 |
|
$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) |
197 |
|
$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) |
207 |
|
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
208 |
|
# var2utf8($result) if ($options->{utf8}); |
209 |
|
|
210 |
|
# destroy persistent object from memory to be sure to get a fresh one next time |
211 |
|
#undef $obj if $options->{destroy}; |
212 |
|
#$options->{cb_destroy}->($obj) if $options->{cb_destroy}; |
213 |
|
#$options->{cb}->{destroy}->($obj) if $options->{destroy}; |
214 |
|
|
215 |
|
return $result; |
216 |
|
} |
217 |
|
|
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 getStructSlotByStringyAddress { |
sub merge_to { |
266 |
my $var = shift; |
_hash2object(@_); |
267 |
my $indexstack_ref = shift; |
# TODO: |
268 |
my @indexstack = @{$indexstack_ref}; |
# re-implement using CPAN's "Iterate". |
269 |
my $joiner = '->'; |
} |
270 |
my @evlist; |
|
271 |
foreach (@indexstack) { |
|
272 |
my $elem; |
# todo: maybe do diff(s) here sometimes!? |
273 |
if ($_ =~ m/\d+/) { $elem = "[$_]"; } |
sub _hash2object { |
274 |
if ($_ =~ m/\D+/) { $elem = "{$_}"; } |
my $object = shift; |
275 |
push @evlist, $elem; |
my $data = shift; |
276 |
} |
my $options = shift; |
277 |
my $evstring = join($joiner, @evlist); |
|
278 |
$evstring = 'return $var->' . $evstring . ';'; |
#print "hash2object", "\n"; |
279 |
#print "ev: $evstring\n"; |
|
280 |
return eval($evstring); |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
281 |
|
# this method corrects this |
282 |
|
numhash2list($data) if ($options->{php}); |
283 |
|
|
284 |
|
# utf8-conversion/-encoding (essential for I18N) |
285 |
|
var_utf2iso($data) if ($options->{utf8}); |
286 |
|
|
287 |
|
# get fresh object from database |
288 |
|
# todo: |
289 |
|
# - handle "locked" objects !!! |
290 |
|
# - check if already another update occoured (object-revisioning needed!) |
291 |
|
#my $obj = $self->getObject($oid); |
292 |
|
|
293 |
|
# mix changes into fresh object and save it back |
294 |
|
_hash2object_traverse_mixin($object, $data, 0, $options); |
295 |
|
|
296 |
|
# done in core mixin function? |
297 |
|
#$self->{storage}->update($obj); |
298 |
|
|
299 |
|
# we should "undef" some objects here to destroy them in memory and enforce reloading when used next time |
300 |
|
# simulate: |
301 |
|
#$self->{storage}->disconnect(); |
302 |
|
|
303 |
|
} |
304 |
|
|
305 |
|
# ------------------------------------ |
306 |
|
# core mixin function |
307 |
|
# TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data |
308 |
|
|
309 |
|
# remember keys of structures we are traversing |
310 |
|
# this is a HACK: |
311 |
|
# - don't (!!!) "packageglobal" @indexstack |
312 |
|
# - it will lead to problems with parallelism! |
313 |
|
|
314 |
|
#my @indexstack; |
315 |
|
|
316 |
|
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
317 |
|
sub _hash2object_traverse_mixin { |
318 |
|
my $object = shift; |
319 |
|
my $data = shift; |
320 |
|
my $bool_recursion = shift; |
321 |
|
my $options = shift; |
322 |
|
|
323 |
|
# clear our key - stack if we are called from user-code (non-recursively) |
324 |
|
@indexstack = () if (!$bool_recursion); |
325 |
|
|
326 |
|
my $classname = ref $object; |
327 |
|
# if ($classname) { |
328 |
|
# print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n"; |
329 |
|
# } |
330 |
|
|
331 |
|
# parser: detected OBJECT (a Tangram one?) (reftype == HASH) (assume) |
332 |
|
# what's exactly done here? hmmm.... it works ;) |
333 |
|
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
334 |
|
# extend! check! |
335 |
|
|
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"; |
346 |
|
|
347 |
|
my @fields; |
348 |
|
# loop through fields of object (Tangram-object) |
349 |
|
@fields = keys %{$object}; |
350 |
|
|
351 |
|
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
352 |
|
@fields = keys %{$data} if $options->{init}; |
353 |
|
# @fields = keys %{$data} if $options->{mixin}; |
354 |
|
|
355 |
|
foreach (@fields) { |
356 |
|
my $field = $_; |
357 |
|
push @indexstack, $field; |
358 |
|
|
359 |
|
# determine type of object |
360 |
|
my $ref = ref $object->{$field}; |
361 |
|
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
362 |
|
if ($ref) { |
363 |
|
_hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
364 |
|
} else { |
365 |
|
my $val = ref_slot($data, \@indexstack); |
366 |
|
|
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; |
378 |
|
} |
379 |
|
|
380 |
|
# save object to database ... |
381 |
|
# ... do an update if it already exists, do an insert if it doesn't |
382 |
|
# my $objectId = $self->{storage}->id($obj); |
383 |
|
# $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" ); |
384 |
|
# if ($objectId) { |
385 |
|
# $self->{storage}->update($obj); |
386 |
|
|
387 |
|
#print __PACKAGE__ . ":", "\n"; |
388 |
|
#print Dumper($object); |
389 |
|
|
390 |
|
# } else { |
391 |
|
# $self->{storage}->insert($obj); |
392 |
|
# } |
393 |
|
|
394 |
|
} |
395 |
|
|
396 |
|
#&& ( ref($obj) ne 'HASH' ) |
397 |
|
|
398 |
|
# loop through entries of array (IntrArray, isn't it?) |
399 |
|
if ((ref $object) eq 'ARRAY') { |
400 |
|
# print STDERR "===", "refttype ", attributes::reftype($obj), "\n"; |
401 |
|
my $i = 0; |
402 |
|
foreach (@{$object}) { |
403 |
|
push @indexstack, $i; |
404 |
|
my $ref = ref $_; |
405 |
|
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
406 |
|
# if ($ref && $_) { |
407 |
|
if ($ref) { |
408 |
|
_hash2object_traverse_mixin($_, $data, 1, $options); |
409 |
|
} else { |
410 |
|
$object->[$i] = $_ if defined $_; |
411 |
|
} |
412 |
|
pop @indexstack; |
413 |
|
$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 |
|
# ACK's go to Randal L. Schwartz <merlyn@stonehenge.com> |
430 |
|
# on the website: |
431 |
|
# This text is copyright by Miller-Freeman, Inc., and is used with their permission. |
432 |
|
# Further distribution or use is not permitted. |
433 |
|
# please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html |
434 |
|
sub deep_copy1 { |
435 |
|
my $this = shift; |
436 |
|
if (not ref $this) { |
437 |
|
$this; |
438 |
|
} elsif (ref $this eq "ARRAY") { |
439 |
|
[map deep_copy1($_), @$this]; |
440 |
|
} elsif (ref $this eq "HASH") { |
441 |
|
+{map { $_ => deep_copy1($this->{$_}) } keys %$this}; |
442 |
|
} elsif (ref $this eq "CODE") { |
443 |
|
$this; |
444 |
|
} else { die "deep_copy1 asks: what type is $this?" } |
445 |
|
#} else { print "deep_copy asks: what type is $this?", "\n"; } |
446 |
|
} |
447 |
|
|
448 |
|
# ACK's go to Andrew Pitonyak |
449 |
|
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
450 |
|
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
451 |
|
sub deep_copy { |
452 |
|
Pitonyak::DeepCopy::deep_copy(@_); |
453 |
} |
} |
454 |
|
|
455 |
|
|
456 |
1; |
1; |
457 |
|
__END__ |