/[cvs]/nfo/perl/libs/Data/Mungle/Transform/Deep.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Mungle/Transform/Deep.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by joko, Tue Dec 3 05:34:55 2002 UTC revision 1.12 by joko, Thu Feb 20 22:45:19 2003 UTC
# Line 1  Line 1 
1  ##############################################  ## ---------------------------------------------------------------------------
2  #  ##  $Id$
3  #  $Id$  ## ---------------------------------------------------------------------------
4  #  ##  $Log$
5  #  $Log$  ##  Revision 1.12  2003/02/20 22:45:19  joko
6  #  Revision 1.3  2002/12/03 05:34:55  joko  ##  fix regarding new deep_copy
7  #  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode  ##
8  #  ##  Revision 1.11  2003/02/20 21:13:54  joko
9  #  Revision 1.2  2002/12/01 04:44:07  joko  ##  - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository)
10  #  + code from Data::Transform::OO  ##
11  #  ##  Revision 1.10  2003/02/20 20:48:00  joko
12  #  Revision 1.1  2002/11/29 04:49:20  joko  ##  - refactored lots of code to Data::Code::Ref
13  #  + initial check-in  ##  + alternative 'deep_copy' implementation
14  #  ##
15  #  Revision 1.1  2002/10/10 03:26:00  cvsjoko  ##  Revision 1.9  2003/02/18 19:35:56  joko
16  #  + new  ##  +- 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;
# Line 26  use warnings; Line 54  use warnings;
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
   &object2hash  
   &hash2object  
   &refexpr2perlref  
60  );  );
61  #  &getStructSlotByStringyAddress  
62    
63  use attributes;  use attributes;
64  use Data::Dumper;  use Data::Dumper;
65  use Data::Transform::Encode qw( var_utf2iso );  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') {
# Line 55  sub var_NumericalHashToArray { Line 84  sub var_NumericalHashToArray {
84                }                }
85                $vref->{$_} = \@arr;                $vref->{$_} = \@arr;
86              }              }
87              var_NumericalHashToArray($vref->{$_});              numhash2list($vref->{$_});
88            }            }
89  #      } else {  #      } else {
90  #        #$vref->{$_} = scalar2iso($vref->{$_});  #        #$vref->{$_} = scalar2iso($vref->{$_});
# Line 65  sub var_NumericalHashToArray { Line 94  sub var_NumericalHashToArray {
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'; }"  ???    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
118    } else {    } else {
# Line 83  sub var_deref { Line 120  sub var_deref {
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;
# Line 140  sub var_traverse_mixin_update { Line 262  sub var_traverse_mixin_update {
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".
   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;
# Line 158  sub hash2object { Line 279  sub hash2object {
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});
# Line 170  sub hash2object { Line 291  sub hash2object {
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);    _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);
# Line 193  sub hash2object { Line 314  sub hash2object {
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;
321      my $options = shift;
322    
323    # 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)
324    @indexstack = () if (!$bool_recursion);    @indexstack = () if (!$bool_recursion);
# Line 210  sub hash2object_traverse_mixin { Line 332  sub hash2object_traverse_mixin {
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                    
347        my @fields;
348      # loop through fields of object (Tangram-object)      # loop through fields of object (Tangram-object)
349      foreach (keys %{$object}) {      @fields = keys %{$object};
350        push @indexstack, $_;  
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        # 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);          _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;                  
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      }      }
# Line 256  sub hash2object_traverse_mixin { Line 403  sub hash2object_traverse_mixin {
403        push @indexstack, $i;        push @indexstack, $i;
404        my $ref = ref $_;        my $ref = ref $_;
405  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
406        if ($ref && $_) {  #      if ($ref && $_) {
407          hash2object_traverse_mixin($_, $data, 1);        if ($ref) {
408            _hash2object_traverse_mixin($_, $data, 1, $options);
409        } else {        } else {
410          $object->[$i] = $_;          $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  sub getStructSlotByStringyAddress {  # on the website:
431    my $var = shift;  # This text is copyright by Miller-Freeman, Inc., and is used with their permission.
432    my $indexstack_ref = shift;  # Further distribution or use is not permitted.
433    my @indexstack = @{$indexstack_ref};  # please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html
434    my $joiner = '->';  sub deep_copy1 {
435    my @evlist;    my $this = shift;
436    foreach (@indexstack) {    if (not ref $this) {
437      my $elem;      $this;
438      if ($_ =~ m/\d+/) { $elem = "[$_]"; }    } elsif (ref $this eq "ARRAY") {
439      if ($_ =~ m/\D+/) { $elem = "{$_}"; }      [map deep_copy1($_), @$this];
440      push @evlist, $elem;    } elsif (ref $this eq "HASH") {
441    }      +{map { $_ => deep_copy1($this->{$_}) } keys %$this};
442    my $evstring = join($joiner, @evlist);    } elsif (ref $this eq "CODE") {
443    $evstring = 'return $var->' . $evstring . ';';      $this;
444    #print "ev: $evstring\n";    } else { die "deep_copy1 asks: what type is $this?" }
445    return eval($evstring);    #} else { print "deep_copy asks: what type is $this?", "\n"; }
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__

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.12

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed