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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Thu Feb 20 20:48:36 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.2: +29 -25 lines
renamed methods

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

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