/[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.10 by joko, Thu Feb 20 20:48:00 2003 UTC revision 1.18 by jonen, Fri Mar 28 03:07:26 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.18  2003/03/28 03:07:26  jonen
6    ##  + minor changes
7    ##
8    ##  Revision 1.17  2003/03/27 15:17:07  joko
9    ##  namespace fixes for Data::Mungle::*
10    ##
11    ##  Revision 1.16  2003/03/27 15:04:52  joko
12    ##  minor update: comment
13    ##
14    ##  Revision 1.15  2003/02/27 14:39:48  jonen
15    ##  + fixed bug at _hash2object()
16    ##
17    ##  Revision 1.14  2003/02/22 17:13:55  jonen
18    ##  + added function 'childObject2string()' to encode 'child'-references to option related string
19    ##  + use new option at 'expand()' for replacing 'childObject2string'
20    ##
21    ##  Revision 1.13  2003/02/21 01:48:50  joko
22    ##  renamed core function
23    ##
24    ##  Revision 1.12  2003/02/20 22:45:19  joko
25    ##  fix regarding new deep_copy
26    ##
27    ##  Revision 1.11  2003/02/20 21:13:54  joko
28    ##  - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository)
29    ##
30  ##  Revision 1.10  2003/02/20 20:48:00  joko  ##  Revision 1.10  2003/02/20 20:48:00  joko
31  ##  - refactored lots of code to Data::Code::Ref  ##  - refactored lots of code to Data::Code::Ref
32  ##  + alternative 'deep_copy' implementation  ##  + alternative 'deep_copy' implementation
# Line 40  Line 65 
65  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
66    
67    
68  package Data::Transform::Deep;  package Data::Mungle::Transform::Deep;
69    
70  use strict;  use strict;
71  use warnings;  use warnings;
# Line 58  use attributes; Line 83  use attributes;
83  use Data::Dumper;  use Data::Dumper;
84  use Iterate;  use Iterate;
85    
86  use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar );  use Pitonyak::DeepCopy;
87  use Data::Code::Ref qw( ref_slot );  use Data::Mungle::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar );
88    use Data::Mungle::Code::Ref qw( ref_slot );
89    
90  sub numhash2list {  sub numhash2list {
91    my $vref = shift;    my $vref = shift;
# Line 129  sub _var_deref_test { Line 155  sub _var_deref_test {
155  }  }
156    
157    
158    # convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML
159    # but still using the known latin locale stuff
160  sub expand {  sub expand {
161    
162    my $obj = shift;    my $obj = shift;
# Line 143  sub expand { Line 171  sub expand {
171        my $item;        my $item;
172        # if current item is a reference ...        # if current item is a reference ...
173        if (ref $_[0]) {        if (ref $_[0]) {
174          # ... expand structure recursively          $item = $_[0];
175          $item = expand($_[0], $options);          # if $options->{childObj2string} is TRUE or STRING don't expand referenced object,
176            # instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> )
177            if ($item && $options->{childObj2string}) {
178              $item = childObj2string($item, $options->{childObj2string});
179            } else {
180              # ... expand structure recursively
181              $item = expand($_[0], $options);
182            }
183          # destroy item via seperate callback method (a POST) if requested          # destroy item via seperate callback method (a POST) if requested
184          #$options->{cb}->{destroy}->($_[0]) if $options->{destroy};          #$options->{cb}->{destroy}->($_[0]) if $options->{destroy};
185    
# Line 173  sub expand { Line 208  sub expand {
208                
209        # if current item is a reference ...        # if current item is a reference ...
210        if (ref $_[1]) {        if (ref $_[1]) {
211          # ... expand structure recursively          $item = $_[1];
212          $item = expand($_[1], $options);          # if $options->{childObj2string} is TRUE or STRING don't expand referenced object,
213            # instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> )
214            if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH")) {
215              $item = childObj2string($item, $options->{childObj2string});
216            } else {
217              # ... expand structure recursively
218              $item = expand($_[1], $options);
219            }
220          # destroy item via seperate callback method (a POST) if requested          # destroy item via seperate callback method (a POST) if requested
221          #$options->{cb}->{destroy}->($_[1]) if $options->{destroy};          #$options->{cb}->{destroy}->($_[1]) if $options->{destroy};
222                
# Line 198  sub expand { Line 240  sub expand {
240    
241    # convert all values to utf8 (inside complex struct)    # convert all values to utf8 (inside complex struct)
242      # 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
243      # var2utf8($result) if ($options->{utf8});      # latin_to_utf8($result) if ($options->{utf8});
244    
245    # 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
246    #undef $obj if $options->{destroy};    #undef $obj if $options->{destroy};
# Line 258  sub _var_traverse_mixin_update_old { Line 300  sub _var_traverse_mixin_update_old {
300  sub merge_to {  sub merge_to {
301    _hash2object(@_);    _hash2object(@_);
302    # TODO:    # TODO:
303    # re-implement using CPAN's "Iterate".    # re-implement using CPAN's "Iterate" and/or a modified Hash::Merge.
304  }  }
305    
306    
# Line 275  sub _hash2object { Line 317  sub _hash2object {
317    numhash2list($data) if ($options->{php});    numhash2list($data) if ($options->{php});
318    
319    # utf8-conversion/-encoding (essential for I18N)    # utf8-conversion/-encoding (essential for I18N)
320    var_utf2iso($data) if ($options->{utf8});    utf8_to_latin($data) if ($options->{utf8});
321    
322    # get fresh object from database    # get fresh object from database
323    # todo:    # todo:
# Line 441  sub deep_copy1 { Line 483  sub deep_copy1 {
483  # ACK's go to Andrew Pitonyak  # ACK's go to Andrew Pitonyak
484  # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org)  # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org)
485  # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html  # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html
486  sub deep_copy2 {  sub deep_copy {
487      Pitonyak::DeepCopy::deep_copy(@_);
488      # if not defined then return it  }
     return undef if $#_ < 0 || !defined( $_[0] );  
   
     # if not a reference then return the parameter  
     return $_[0] if !ref( $_[0] );  
     my $obj = shift;  
     if ( UNIVERSAL::isa( $obj, 'SCALAR' ) ) {  
         my $temp = deep_copy2($$obj);  
         return \$temp;  
     }  
     elsif ( UNIVERSAL::isa( $obj, 'HASH' ) ) {  
         my $temp_hash = {};  
         foreach my $key ( keys %$obj ) {  
             if ( !defined( $obj->{$key} ) || !ref( $obj->{$key} ) ) {  
                 $temp_hash->{$key} = $obj->{$key};  
             }  
             else {  
                 $temp_hash->{$key} = deep_copy2( $obj->{$key} );  
             }  
         }  
         return $temp_hash;  
     }  
     elsif ( UNIVERSAL::isa( $obj, 'ARRAY' ) ) {  
         my $temp_array = [];  
         foreach my $array_val (@$obj) {  
             if ( !defined($array_val) || !ref($array_val) ) {  
                 push ( @$temp_array, $array_val );  
             }  
             else {  
                 push ( @$temp_array, deep_copy2($array_val) );  
             }  
         }  
         return $temp_array;  
     }  
489    
     # ?? I am uncertain about this one  
     elsif ( UNIVERSAL::isa( $obj, 'REF' ) ) {  
         my $temp = deep_copy2($$obj);  
         return \$temp;  
     }  
490    
491      # I guess that it is either CODE, GLOB or LVALUE  sub childObj2string {
492      else {    my $obj = shift;
493          return $obj;    my $option = shift;
494      }    my $classname = ref $obj;
495      
496      if($option == 1) {
497        my $string = "o_" . $obj->{guid} . "_" . $classname;
498      }  
499      return $string;
500  }  }
501    
 sub deep_copy {  
   deep_copy2(@_);  
 }  
502    
503  1;  1;
504  __END__  __END__

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.18

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