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

Annotation of /nfo/perl/libs/Data/Mungle/Transform/Encode.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Wed Apr 9 07:22:34 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.4: +35 -2 lines
childObj2string now inside Encode.pm, renamed to 'twingle_reference'

1 joko 1.3 ## -------------------------------------------------------------------------
2 joko 1.5 ## $Id: Encode.pm,v 1.4 2003/03/27 15:17:07 joko Exp $
3 joko 1.3 ## -------------------------------------------------------------------------
4     ## $Log: Encode.pm,v $
5 joko 1.5 ## Revision 1.4 2003/03/27 15:17:07 joko
6     ## namespace fixes for Data::Mungle::*
7     ##
8 joko 1.4 ## Revision 1.3 2003/02/20 20:48:36 joko
9     ## renamed methods
10     ##
11 joko 1.3 ## Revision 1.2 2002/12/05 13:57:22 joko
12     ## + now able to export 'scalar2utf8'
13     ## + comments
14     ##
15     ## Revision 1.1 2002/11/29 04:49:20 joko
16     ## + initial check-in
17     ##
18     ## Revision 1.1 2002/10/10 03:26:00 cvsjoko
19     ## + new
20     ## -------------------------------------------------------------------------
21 joko 1.1
22    
23 joko 1.4 package Data::Mungle::Transform::Encode;
24 joko 1.1
25     use strict;
26     use warnings;
27    
28     require Exporter;
29     our @ISA = qw( Exporter );
30 joko 1.5 our @EXPORT_OK = qw(
31     &latin_to_utf8
32     &latin_to_utf8_scalar
33     &utf8_to_latin
34     &utf8_to_latin_scalar
35     &twingle_reference
36     );
37 joko 1.3
38 joko 1.1
39     use Unicode::String qw(utf8 latin1);
40    
41 joko 1.2 # TODO: refactor using Greg London's "Iterate" from CPAN
42 joko 1.3 sub latin_to_utf8 {
43 joko 1.1 my $vref = shift;
44     if (ref $vref eq 'HASH') {
45     foreach (keys %{$vref}) {
46     if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) {
47 joko 1.3 latin_to_utf8($vref->{$_});
48 joko 1.1 } else {
49 joko 1.3 $vref->{$_} = latin_to_utf8_scalar($vref->{$_});
50 joko 1.1 }
51     }
52     } elsif (ref $vref eq 'ARRAY') {
53     foreach (@{$vref}) {
54     if (ref $_ ne 'SCALAR') {
55 joko 1.3 latin_to_utf8($_);
56 joko 1.1 } else {
57 joko 1.3 $_ = latin_to_utf8_scalar($_);
58 joko 1.1 }
59     }
60     }
61    
62     }
63    
64 joko 1.3 sub latin_to_utf8_scalar {
65 joko 1.1 my $scalar = shift;
66     if ($scalar) {
67     my $u = latin1($scalar);
68     return $u->utf8;
69     }
70     }
71    
72 joko 1.2 # TODO: refactor using Greg London's "Iterate" from CPAN
73 joko 1.3 sub utf8_to_latin {
74 joko 1.1 my $vref = shift;
75     if (ref $vref eq 'HASH') {
76     foreach (keys %{$vref}) {
77     if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) {
78 joko 1.3 utf8_to_latin($vref->{$_});
79 joko 1.1 } else {
80 joko 1.3 $vref->{$_} = utf8_to_latin_scalar($vref->{$_});
81 joko 1.1 }
82     }
83     } elsif (ref $vref eq 'ARRAY') {
84     foreach (@{$vref}) {
85     if (ref $_ ne 'SCALAR') {
86 joko 1.3 utf8_to_latin($_);
87 joko 1.1 } else {
88 joko 1.3 $_ = utf8_to_latin_scalar($_);
89 joko 1.1 }
90     }
91     }
92    
93     }
94    
95 joko 1.3 sub utf8_to_latin_scalar {
96 joko 1.1 my $scalar = shift;
97     if ($scalar) {
98     my $u = utf8($scalar);
99     return $u->latin1;
100     }
101     }
102 joko 1.5
103    
104     # encodes object-references to serialized string representations
105     # made up of:
106     # - 'o_<classname>_<ref type>_<guid>'???
107     # - 'o_{guid}_{classname}'!!!
108    
109     # TODO: enhance further!
110     # make it possible to twingle OID-, GUID- and/or other references
111    
112     sub twingle_reference {
113     my $obj = shift;
114     my $option = shift;
115     my $string = 'n/a';
116    
117     #if ($option == 1) {
118     if ((my $classname = ref $obj) && (my $guid = $obj->{guid})) {
119     $string = join('_', 'o', $guid, $classname);
120     }
121     #}
122    
123     return $string;
124     }
125    
126 joko 1.1
127     1;
128 joko 1.3 __END__

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