2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.11 2003/02/20 21:13:54 joko |
6 |
|
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
7 |
|
## |
8 |
|
## Revision 1.10 2003/02/20 20:48:00 joko |
9 |
|
## - refactored lots of code to Data::Code::Ref |
10 |
|
## + alternative 'deep_copy' implementation |
11 |
|
## |
12 |
|
## Revision 1.9 2003/02/18 19:35:56 joko |
13 |
|
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
14 |
|
## |
15 |
|
## Revision 1.8 2003/02/09 05:10:56 joko |
16 |
|
## + minor update |
17 |
|
## |
18 |
## Revision 1.7 2003/01/19 03:26:59 joko |
## Revision 1.7 2003/01/19 03:26:59 joko |
19 |
## + added 'sub deep_copy' - refactored from libp |
## + added 'sub deep_copy' - refactored from libp |
20 |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
51 |
require Exporter; |
require Exporter; |
52 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
53 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
|
&var_NumericalHashToArray |
|
|
&var_mixin |
|
|
&object2hash |
|
|
&hash2object |
|
|
&refexpr2perlref |
|
54 |
&expand |
&expand |
55 |
&deep_copy |
&deep_copy |
56 |
&merge |
&merge_to |
57 |
); |
); |
58 |
# &var_deref |
|
|
# &getStructSlotByStringyAddress |
|
59 |
|
|
60 |
use attributes; |
use attributes; |
61 |
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 ); |
|
62 |
use Iterate; |
use Iterate; |
63 |
|
|
64 |
sub var_NumericalHashToArray { |
use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar ); |
65 |
|
use Data::Code::Ref qw( ref_slot ); |
66 |
|
|
67 |
|
sub numhash2list { |
68 |
my $vref = shift; |
my $vref = shift; |
69 |
#my $context = shift; |
#my $context = shift; |
70 |
if (ref $vref eq 'HASH') { |
if (ref $vref eq 'HASH') { |
80 |
} |
} |
81 |
$vref->{$_} = \@arr; |
$vref->{$_} = \@arr; |
82 |
} |
} |
83 |
var_NumericalHashToArray($vref->{$_}); |
numhash2list($vref->{$_}); |
84 |
} |
} |
85 |
# } else { |
# } else { |
86 |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
90 |
} |
} |
91 |
|
|
92 |
|
|
93 |
sub var_deref { |
# FIXME: could this be refactored using expand? |
94 |
|
# btw: "expand": look at scripts@CPAN (System Administration): |
95 |
|
# there is a perl make with perl |
96 |
|
sub _var_deref_test { |
97 |
my $obj = shift; |
my $obj = shift; |
98 |
my $options = shift; |
my $options = shift; |
99 |
my $result; |
my $result; |
138 |
my $options = shift; |
my $options = shift; |
139 |
my $result; |
my $result; |
140 |
|
|
141 |
if ((ref $obj) eq 'ARRAY') { |
#print "ref: ", ref $obj, "\n"; |
142 |
|
|
143 |
|
if (ref $obj eq 'ARRAY') { |
144 |
|
|
145 |
IterArray @$obj, sub { |
IterArray @$obj, sub { |
146 |
my $item; |
my $item; |
156 |
#$item = deep_copy($_[0]); |
#$item = deep_copy($_[0]); |
157 |
$item = $_[0]; |
$item = $_[0]; |
158 |
# conversions/encodings |
# conversions/encodings |
159 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
160 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
161 |
} |
} |
162 |
#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) |
163 |
push(@{$result}, $item); # use item in any case |
push(@{$result}, $item); # use item in any case |
164 |
|
|
165 |
} |
} |
166 |
|
|
167 |
|
} elsif (ref $obj eq 'CODE') { |
168 |
|
#print Dumper($obj); |
169 |
|
#exit; |
170 |
|
|
171 |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
172 |
} else { |
} elsif (ref $obj) { |
173 |
|
|
174 |
IterHash %$obj, sub { |
IterHash %$obj, sub { |
175 |
my $item; |
my $item; |
186 |
#$item = deep_copy($_[1]); |
#$item = deep_copy($_[1]); |
187 |
$item = $_[1]; |
$item = $_[1]; |
188 |
# conversions/encodings |
# conversions/encodings |
189 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
190 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
191 |
} |
} |
192 |
#$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) |
193 |
$result->{$_[0]} = $item; # use item in any case |
$result->{$_[0]} = $item; # use item in any case |
194 |
} |
} |
195 |
|
|
196 |
|
} else { |
197 |
|
#die ("not a reference!"); |
198 |
|
$result = $obj; |
199 |
|
|
200 |
} |
} |
201 |
|
|
202 |
# convert all values to utf8 (inside complex struct) |
# convert all values to utf8 (inside complex struct) |
214 |
|
|
215 |
|
|
216 |
my @indexstack; |
my @indexstack; |
217 |
sub var_traverse_mixin_update { |
sub _var_traverse_mixin_update_old { |
218 |
my $obj = shift; |
my $obj = shift; |
219 |
my $data = shift; |
my $data = shift; |
220 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
258 |
#return $result; |
#return $result; |
259 |
} |
} |
260 |
|
|
261 |
sub object2hash { |
sub merge_to { |
262 |
my $object = shift; |
_hash2object(@_); |
263 |
my $options = shift; |
# TODO: |
264 |
#my $deref = var_deref($object); |
# re-implement using CPAN's "Iterate". |
|
my $deref = expand($object, $options); |
|
|
#var2utf8($deref) if ($options->{utf8}); |
|
|
return $deref; |
|
265 |
} |
} |
266 |
|
|
267 |
|
|
268 |
# todo: maybe do diff(s) here sometimes!? |
# todo: maybe do diff(s) here sometimes!? |
269 |
sub hash2object { |
sub _hash2object { |
270 |
my $object = shift; |
my $object = shift; |
271 |
my $data = shift; |
my $data = shift; |
272 |
my $options = shift; |
my $options = shift; |
275 |
|
|
276 |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
277 |
# this method corrects this |
# this method corrects this |
278 |
var_NumericalHashToArray($data) if ($options->{php}); |
numhash2list($data) if ($options->{php}); |
279 |
|
|
280 |
# utf8-conversion/-encoding (essential for I18N) |
# utf8-conversion/-encoding (essential for I18N) |
281 |
var_utf2iso($data) if ($options->{utf8}); |
var_utf2iso($data) if ($options->{utf8}); |
287 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
288 |
|
|
289 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
290 |
hash2object_traverse_mixin($object, $data, 0, $options); |
_hash2object_traverse_mixin($object, $data, 0, $options); |
291 |
|
|
292 |
# done in core mixin function? |
# done in core mixin function? |
293 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
310 |
#my @indexstack; |
#my @indexstack; |
311 |
|
|
312 |
# 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 |
313 |
sub hash2object_traverse_mixin { |
sub _hash2object_traverse_mixin { |
314 |
my $object = shift; |
my $object = shift; |
315 |
my $data = shift; |
my $data = shift; |
316 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
328 |
# what's exactly done here? hmmm.... it works ;) |
# what's exactly done here? hmmm.... it works ;) |
329 |
# 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!!! |
330 |
# extend! check! |
# extend! check! |
331 |
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
|
332 |
|
#print attributes::reftype($object), "\n"; |
333 |
|
|
334 |
|
# V1 |
335 |
|
#if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
336 |
|
# V2 |
337 |
|
my $reftype = attributes::reftype($object); |
338 |
|
#print "reftype: '$reftype'", "\n"; |
339 |
|
if ($reftype eq 'HASH') { |
340 |
|
|
341 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
342 |
|
|
356 |
my $ref = ref $object->{$field}; |
my $ref = ref $object->{$field}; |
357 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
358 |
if ($ref) { |
if ($ref) { |
359 |
hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
_hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
360 |
} else { |
} else { |
361 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = ref_slot($data, \@indexstack); |
362 |
|
|
363 |
print Dumper($options); |
#print Dumper($options); |
364 |
my $field_target = $field; |
my $field_target = $field; |
365 |
if (my $pattern = $options->{pattern_strip_key}) { |
if (my $pattern = $options->{pattern_strip_key}) { |
366 |
print "pattern: $pattern", "\n"; |
print "pattern: $pattern", "\n"; |
401 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
402 |
# if ($ref && $_) { |
# if ($ref && $_) { |
403 |
if ($ref) { |
if ($ref) { |
404 |
hash2object_traverse_mixin($_, $data, 1, $options); |
_hash2object_traverse_mixin($_, $data, 1, $options); |
405 |
} else { |
} else { |
406 |
$object->[$i] = $_ if defined $_; |
$object->[$i] = $_ if defined $_; |
407 |
} |
} |
408 |
pop @indexstack; |
pop @indexstack; |
409 |
$i++; |
$i++; |
410 |
} |
} |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
# 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"; |
|
|
} |
|
|
} |
|
411 |
|
|
412 |
|
=pod |
413 |
|
# object? |
414 |
} else { |
} else { |
415 |
# use expr as simple scalar key (attributename) |
print "reference: ", ref $object, "\n"; |
416 |
$value = $obj->{$expr}; |
print "reftype: ", $reftype, "\n"; |
417 |
} |
die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!"); |
418 |
|
=cut |
|
return $value; |
|
419 |
|
|
420 |
} |
} |
|
|
|
|
|
|
|
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; |
|
421 |
|
|
|
# join parts with dereference operators together again and return built string |
|
|
return (join('->', @parts_capsule), \@parts_pure); |
|
422 |
} |
} |
423 |
|
|
424 |
# ACK's go to ... |
|
425 |
sub deep_copy { |
# ACK's go to Randal L. Schwartz <merlyn@stonehenge.com> |
426 |
|
# on the website: |
427 |
|
# This text is copyright by Miller-Freeman, Inc., and is used with their permission. |
428 |
|
# Further distribution or use is not permitted. |
429 |
|
# please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html |
430 |
|
sub deep_copy1 { |
431 |
my $this = shift; |
my $this = shift; |
432 |
if (not ref $this) { |
if (not ref $this) { |
433 |
$this; |
$this; |
434 |
} elsif (ref $this eq "ARRAY") { |
} elsif (ref $this eq "ARRAY") { |
435 |
[map deep_copy($_), @$this]; |
[map deep_copy1($_), @$this]; |
436 |
} elsif (ref $this eq "HASH") { |
} elsif (ref $this eq "HASH") { |
437 |
+{map { $_ => deep_copy($this->{$_}) } keys %$this}; |
+{map { $_ => deep_copy1($this->{$_}) } keys %$this}; |
438 |
} elsif (ref $this eq "CODE") { |
} elsif (ref $this eq "CODE") { |
439 |
$this; |
$this; |
440 |
#} else { die "deep_copy asks: what type is $this?" } |
} else { die "deep_copy1 asks: what type is $this?" } |
441 |
} else { print "deep_copy asks: what type is $this?", "\n"; } |
#} else { print "deep_copy asks: what type is $this?", "\n"; } |
442 |
|
} |
443 |
|
|
444 |
|
# ACK's go to Andrew Pitonyak |
445 |
|
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
446 |
|
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
447 |
|
sub deep_copy { |
448 |
|
Pitonyak::deep_copy(@_); |
449 |
} |
} |
450 |
|
|
451 |
1; |
1; |
452 |
|
__END__ |