1 |
############################################## |
2 |
# |
3 |
# $Id: Encode.pm,v 1.1 2003/01/18 15:38:05 joko Exp $ |
4 |
# |
5 |
# $Log: Encode.pm,v $ |
6 |
# Revision 1.1 2003/01/18 15:38:05 joko |
7 |
# + initial check-in |
8 |
# |
9 |
# Revision 1.2 2002/12/05 13:57:22 joko |
10 |
# + now able to export 'scalar2utf8' |
11 |
# + comments |
12 |
# |
13 |
# Revision 1.1 2002/11/29 04:49:20 joko |
14 |
# + initial check-in |
15 |
# |
16 |
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
17 |
# + new |
18 |
# |
19 |
############################################## |
20 |
|
21 |
|
22 |
package Data::Transform::Encode; |
23 |
|
24 |
use strict; |
25 |
use warnings; |
26 |
|
27 |
require Exporter; |
28 |
our @ISA = qw( Exporter ); |
29 |
our @EXPORT_OK = qw( &var2utf8 &var_utf2iso &scalar2utf8 ); |
30 |
|
31 |
use Unicode::String qw(utf8 latin1); |
32 |
|
33 |
# TODO: refactor using Greg London's "Iterate" from CPAN |
34 |
sub var2utf8 { |
35 |
my $vref = shift; |
36 |
if (ref $vref eq 'HASH') { |
37 |
foreach (keys %{$vref}) { |
38 |
if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) { |
39 |
var2utf8($vref->{$_}); |
40 |
} else { |
41 |
$vref->{$_} = scalar2utf8($vref->{$_}); |
42 |
} |
43 |
} |
44 |
} elsif (ref $vref eq 'ARRAY') { |
45 |
foreach (@{$vref}) { |
46 |
if (ref $_ ne 'SCALAR') { |
47 |
var2utf8($_); |
48 |
} else { |
49 |
$_ = scalar2utf8($_); |
50 |
} |
51 |
} |
52 |
} |
53 |
|
54 |
} |
55 |
|
56 |
sub scalar2utf8 { |
57 |
my $scalar = shift; |
58 |
if ($scalar) { |
59 |
my $u = latin1($scalar); |
60 |
return $u->utf8; |
61 |
} |
62 |
} |
63 |
|
64 |
# TODO: refactor using Greg London's "Iterate" from CPAN |
65 |
sub var_utf2iso { |
66 |
my $vref = shift; |
67 |
if (ref $vref eq 'HASH') { |
68 |
foreach (keys %{$vref}) { |
69 |
if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) { |
70 |
var_utf2iso($vref->{$_}); |
71 |
} else { |
72 |
$vref->{$_} = scalar2iso($vref->{$_}); |
73 |
} |
74 |
} |
75 |
} elsif (ref $vref eq 'ARRAY') { |
76 |
foreach (@{$vref}) { |
77 |
if (ref $_ ne 'SCALAR') { |
78 |
var_utf2iso($_); |
79 |
} else { |
80 |
$_ = scalar2iso($_); |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
} |
86 |
|
87 |
sub scalar2iso { |
88 |
my $scalar = shift; |
89 |
if ($scalar) { |
90 |
my $u = utf8($scalar); |
91 |
return $u->latin1; |
92 |
} |
93 |
} |
94 |
|
95 |
1; |