3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.6 2002/12/23 11:27:53 jonen |
7 |
|
# + changed behavior WATCH! |
8 |
|
# |
9 |
|
# Revision 1.5 2002/12/16 19:57:54 joko |
10 |
|
# + option 'init' |
11 |
|
# |
12 |
|
# Revision 1.4 2002/12/05 13:56:49 joko |
13 |
|
# - var_deref |
14 |
|
# + expand - more sophisticated dereferencing with callbacks using Iterate |
15 |
|
# |
16 |
# Revision 1.3 2002/12/03 05:34:55 joko |
# Revision 1.3 2002/12/03 05:34:55 joko |
17 |
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
18 |
# |
# |
37 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
38 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
39 |
&var_NumericalHashToArray |
&var_NumericalHashToArray |
|
&var_deref |
|
40 |
&var_mixin |
&var_mixin |
41 |
&object2hash |
&object2hash |
42 |
&hash2object |
&hash2object |
43 |
&refexpr2perlref |
&refexpr2perlref |
44 |
|
&expand |
45 |
); |
); |
46 |
|
# &var_deref |
47 |
# &getStructSlotByStringyAddress |
# &getStructSlotByStringyAddress |
48 |
|
|
49 |
use attributes; |
use attributes; |
50 |
use Data::Dumper; |
use Data::Dumper; |
51 |
use Data::Transform::Encode qw( var_utf2iso ); |
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
52 |
|
use libp qw( deep_copy ); |
53 |
|
use Iterate; |
54 |
|
|
55 |
sub var_NumericalHashToArray { |
sub var_NumericalHashToArray { |
56 |
my $vref = shift; |
my $vref = shift; |
80 |
|
|
81 |
sub var_deref { |
sub var_deref { |
82 |
my $obj = shift; |
my $obj = shift; |
83 |
|
my $options = shift; |
84 |
my $result; |
my $result; |
85 |
if ((ref $obj) eq 'ARRAY') { |
if ((ref $obj) eq 'ARRAY') { |
86 |
foreach (@{$obj}) { |
foreach (@{$obj}) { |
87 |
my $ref = ref $_; |
my $ref = ref $_; |
88 |
if ($ref) { |
if ($ref) { |
89 |
push(@{$result}, var_deref($_)); |
push(@{$result}, var_deref($_, $options)); |
90 |
|
#undef $_; |
91 |
} else { |
} else { |
92 |
push(@{$result}, $_); |
#push(@{$result}, $_); |
93 |
|
push(@{$result}, deep_copy($_)); |
94 |
} |
} |
95 |
|
#undef $_ if $options->{destroy}; |
96 |
|
#$options->{destroy}->($_) if $options->{destroy}; |
97 |
} |
} |
98 |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
99 |
} else { |
} else { |
101 |
my $key = $_; |
my $key = $_; |
102 |
my $ref = ref $obj->{$_}; |
my $ref = ref $obj->{$_}; |
103 |
if ($ref) { |
if ($ref) { |
104 |
$result->{$_} = var_deref($obj->{$_}); |
$result->{$_} = var_deref($obj->{$_}, $options); |
105 |
|
#undef $obj->{$_}; |
106 |
} else { |
} else { |
107 |
$result->{$_} = $obj->{$_}; |
#$result->{$_} = $obj->{$_}; |
108 |
|
$result->{$_} = deep_copy($obj->{$_}); |
109 |
} |
} |
110 |
|
#undef $obj->{$_} if $options->{destroy}; |
111 |
|
#$options->{destroy}->($obj->{$_}) if $options->{destroy}; |
112 |
} |
} |
113 |
} |
} |
114 |
|
#undef $obj if $options->{destroy}; |
115 |
|
$options->{cb_destroy}->($obj) if $options->{cb_destroy}; |
116 |
return $result; |
return $result; |
117 |
} |
} |
118 |
|
|
119 |
|
|
120 |
|
sub expand { |
121 |
|
|
122 |
|
my $obj = shift; |
123 |
|
my $options = shift; |
124 |
|
my $result; |
125 |
|
|
126 |
|
if ((ref $obj) eq 'ARRAY') { |
127 |
|
|
128 |
|
IterArray @$obj, sub { |
129 |
|
my $item; |
130 |
|
# if current item is a reference ... |
131 |
|
if (ref $_[0]) { |
132 |
|
# ... expand structure recursively |
133 |
|
$item = expand($_[0], $options); |
134 |
|
# destroy item via seperate callback method (a POST) if requested |
135 |
|
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
136 |
|
|
137 |
|
# ... assume plain scalar |
138 |
|
} else { |
139 |
|
#$item = deep_copy($_[0]); |
140 |
|
$item = $_[0]; |
141 |
|
# conversions/encodings |
142 |
|
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
143 |
|
} |
144 |
|
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
145 |
|
push(@{$result}, $item); # use item in any case |
146 |
|
|
147 |
|
} |
148 |
|
|
149 |
|
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
150 |
|
} else { |
151 |
|
|
152 |
|
IterHash %$obj, sub { |
153 |
|
my $item; |
154 |
|
|
155 |
|
# if current item is a reference ... |
156 |
|
if (ref $_[1]) { |
157 |
|
# ... expand structure recursively |
158 |
|
$item = expand($_[1], $options); |
159 |
|
# destroy item via seperate callback method (a POST) if requested |
160 |
|
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
161 |
|
|
162 |
|
# ... assume plain scalar |
163 |
|
} else { |
164 |
|
#$item = deep_copy($_[1]); |
165 |
|
$item = $_[1]; |
166 |
|
# conversions/encodings |
167 |
|
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
168 |
|
} |
169 |
|
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
170 |
|
$result->{$_[0]} = $item; # use item in any case |
171 |
|
} |
172 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
# convert all values to utf8 (inside complex struct) |
176 |
|
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
177 |
|
# var2utf8($result) if ($options->{utf8}); |
178 |
|
|
179 |
|
# destroy persistent object from memory to be sure to get a fresh one next time |
180 |
|
#undef $obj if $options->{destroy}; |
181 |
|
#$options->{cb_destroy}->($obj) if $options->{cb_destroy}; |
182 |
|
#$options->{cb}->{destroy}->($obj) if $options->{destroy}; |
183 |
|
|
184 |
|
return $result; |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
my @indexstack; |
my @indexstack; |
234 |
sub object2hash { |
sub object2hash { |
235 |
my $object = shift; |
my $object = shift; |
236 |
my $options = shift; |
my $options = shift; |
237 |
my $deref = var_deref($object); |
#my $deref = var_deref($object); |
238 |
var2utf8($deref) if ($options->{utf8}); |
my $deref = expand($object, $options); |
239 |
|
#var2utf8($deref) if ($options->{utf8}); |
240 |
return $deref; |
return $deref; |
241 |
} |
} |
242 |
|
|
262 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
263 |
|
|
264 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
265 |
hash2object_traverse_mixin($object, $data); |
hash2object_traverse_mixin($object, $data, 0, $options); |
266 |
|
|
267 |
# done in core mixin function? |
# done in core mixin function? |
268 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
289 |
my $object = shift; |
my $object = shift; |
290 |
my $data = shift; |
my $data = shift; |
291 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
292 |
|
my $options = shift; |
293 |
|
|
294 |
# clear our key - stack if we are called from user-code (non-recursively) |
# clear our key - stack if we are called from user-code (non-recursively) |
295 |
@indexstack = () if (!$bool_recursion); |
@indexstack = () if (!$bool_recursion); |
307 |
|
|
308 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
309 |
|
|
310 |
|
my @fields; |
311 |
# loop through fields of object (Tangram-object) |
# loop through fields of object (Tangram-object) |
312 |
foreach (keys %{$object}) { |
@fields = keys %{$object}; |
313 |
|
|
314 |
|
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
315 |
|
@fields = keys %{$data} if $options->{init}; |
316 |
|
# @fields = keys %{$data} if $options->{mixin}; |
317 |
|
|
318 |
|
foreach (@fields) { |
319 |
push @indexstack, $_; |
push @indexstack, $_; |
320 |
|
|
321 |
# determine type of object |
# determine type of object |
322 |
my $ref = ref $object->{$_}; |
my $ref = ref $object->{$_}; |
323 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
324 |
if ($ref) { |
if ($ref) { |
325 |
hash2object_traverse_mixin($object->{$_}, $data, 1); |
hash2object_traverse_mixin($object->{$_}, $data, 1, $options); |
326 |
} else { |
} else { |
327 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
328 |
$object->{$_} = $val; |
$object->{$_} = $val if defined $val; |
329 |
} |
} |
330 |
pop @indexstack; |
pop @indexstack; |
331 |
} |
} |
356 |
push @indexstack, $i; |
push @indexstack, $i; |
357 |
my $ref = ref $_; |
my $ref = ref $_; |
358 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
359 |
if ($ref && $_) { |
# if ($ref && $_) { |
360 |
hash2object_traverse_mixin($_, $data, 1); |
if ($ref) { |
361 |
|
hash2object_traverse_mixin($_, $data, 1, $options); |
362 |
} else { |
} else { |
363 |
$object->[$i] = $_; |
$object->[$i] = $_ if defined $_; |
364 |
} |
} |
365 |
pop @indexstack; |
pop @indexstack; |
366 |
$i++; |
$i++; |
371 |
|
|
372 |
|
|
373 |
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
374 |
|
# TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ? ;-) |
375 |
sub getStructSlotByStringyAddress { |
sub getStructSlotByStringyAddress { |
376 |
my $var = shift; |
my $var = shift; |
377 |
my $indexstack_ref = shift; |
my $indexstack_ref = shift; |