1 |
## ------------------------------------------------------------------------- |
2 |
## $Id: Encode.pm,v 1.4 2003/03/27 15:17:07 joko Exp $ |
3 |
## ------------------------------------------------------------------------- |
4 |
## $Log: Encode.pm,v $ |
5 |
## Revision 1.4 2003/03/27 15:17:07 joko |
6 |
## namespace fixes for Data::Mungle::* |
7 |
## |
8 |
## Revision 1.3 2003/02/20 20:48:36 joko |
9 |
## renamed methods |
10 |
## |
11 |
## 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 |
|
22 |
|
23 |
package Data::Mungle::Transform::Encode; |
24 |
|
25 |
use strict; |
26 |
use warnings; |
27 |
|
28 |
require Exporter; |
29 |
our @ISA = qw( Exporter ); |
30 |
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 |
|
38 |
|
39 |
use Unicode::String qw(utf8 latin1); |
40 |
|
41 |
# TODO: refactor using Greg London's "Iterate" from CPAN |
42 |
sub latin_to_utf8 { |
43 |
my $vref = shift; |
44 |
if (ref $vref eq 'HASH') { |
45 |
foreach (keys %{$vref}) { |
46 |
if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) { |
47 |
latin_to_utf8($vref->{$_}); |
48 |
} else { |
49 |
$vref->{$_} = latin_to_utf8_scalar($vref->{$_}); |
50 |
} |
51 |
} |
52 |
} elsif (ref $vref eq 'ARRAY') { |
53 |
foreach (@{$vref}) { |
54 |
if (ref $_ ne 'SCALAR') { |
55 |
latin_to_utf8($_); |
56 |
} else { |
57 |
$_ = latin_to_utf8_scalar($_); |
58 |
} |
59 |
} |
60 |
} |
61 |
|
62 |
} |
63 |
|
64 |
sub latin_to_utf8_scalar { |
65 |
my $scalar = shift; |
66 |
if ($scalar) { |
67 |
my $u = latin1($scalar); |
68 |
return $u->utf8; |
69 |
} |
70 |
} |
71 |
|
72 |
# TODO: refactor using Greg London's "Iterate" from CPAN |
73 |
sub utf8_to_latin { |
74 |
my $vref = shift; |
75 |
if (ref $vref eq 'HASH') { |
76 |
foreach (keys %{$vref}) { |
77 |
if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) { |
78 |
utf8_to_latin($vref->{$_}); |
79 |
} else { |
80 |
$vref->{$_} = utf8_to_latin_scalar($vref->{$_}); |
81 |
} |
82 |
} |
83 |
} elsif (ref $vref eq 'ARRAY') { |
84 |
foreach (@{$vref}) { |
85 |
if (ref $_ ne 'SCALAR') { |
86 |
utf8_to_latin($_); |
87 |
} else { |
88 |
$_ = utf8_to_latin_scalar($_); |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
} |
94 |
|
95 |
sub utf8_to_latin_scalar { |
96 |
my $scalar = shift; |
97 |
if ($scalar) { |
98 |
my $u = utf8($scalar); |
99 |
return $u->latin1; |
100 |
} |
101 |
} |
102 |
|
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 |
|
127 |
1; |
128 |
__END__ |