File Coverage

lib/Egg/Plugin/Encode.pm
Criterion Covered Total %
statement 27 66 40.9
branch 0 14 0.0
condition 0 3 0.0
subroutine 9 19 47.3
pod 4 4 100.0
total 40 106 37.7


line stmt bran cond sub pod time code
1             package Egg::Plugin::Encode;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Encode.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   525 use strict;
  1         3  
  1         41  
8 1     1   6 use warnings;
  1         3  
  1         67  
9              
10             our $VERSION= '3.01';
11              
12             sub _setup {
13 0     0     my($e)= @_;
14 1     1   5 no warnings 'redefine';
  1         3  
  1         81  
15 0 0         if (my $icode= $e->config->{character_in}) {
16 0           my $class= $e->global->{request_class};
17             ## my $code = $class->can('parameters')
18             ## || \&Egg::Request::handler::parameters;
19 0           my $code = \&Egg::Request::handler::parameters;
20 1     1   6 no strict 'refs'; ## no critic.
  1         2  
  1         28  
21 1     1   4 no warnings 'redefine';
  1         2  
  1         140  
22 0           *{"${class}::parameters"}= sub {
23 0   0 0     $_[0]->{parameters} ||= do {
24 0           tie my %param,
25             'Egg::Plugin::Encode::TieHash', $e, $icode, $code->(@_);
26 0           \%param;
27             };
28 0           };
29 0           *{"${class}::params"}= $class->can('parameters');
  0            
30             }
31 0           my $encode= $e->create_encode;
32 1     1   7 no warnings 'redefine';
  1         2  
  1         197  
33 0     0     *encode= sub { $encode };
  0            
34 0           $e->next::method;
35             }
36              
37             sub create_encode {
38 0     0 1   require Jcode;
39 0           Jcode->new('jcode context.');
40             }
41 0     0 1   sub utf8_conv { shift->encode->set(@_)->utf8 }
42 0     0 1   sub sjis_conv { shift->encode->set(@_)->sjis }
43 0     0 1   sub euc_conv { shift->encode->set(@_)->euc }
44              
45             package Egg::Plugin::Encode::TieHash;
46 1     1   6 use strict;
  1         2  
  1         24  
47 1     1   5 use warnings;
  1         2  
  1         30  
48 1     1   5 use Tie::Hash;
  1         390  
  1         12  
49              
50             our @ISA= 'Tie::ExtraHash';
51              
52             my $conv;
53              
54             sub TIEHASH {
55 0     0     my($class, $e, $icode, $param)= @_;
56 0           $conv= "${icode}_conv";
57 0           bless [$param, $e, {}], $class;
58             }
59             sub FETCH {
60 0     0     my($self, $key)= @_;
61 0 0         return "" unless exists($self->[0]{$key});
62 0 0         return $self->[0]{$key} if $self->[2]{$key};
63 0           $self->[2]{$key}= 1;
64 0           my $value= \$self->[0]{$key};
65 0 0         if (ref($$value) eq 'Fh') {
    0          
66 0           return $$value;
67             } elsif (ref($$value) eq 'ARRAY') {
68 0           for (@$value) { tr/\r//d; $self->[1]->$conv(\$_) }
  0            
  0            
69 0 0         return wantarray ? @$value: $value;
70             }else {
71 0           $$value=~tr/\r//d;
72 0           return $$value= $self->[1]->$conv($value);
73             }
74             }
75             sub STORE {
76 0     0     my($self, $key)= splice @_, 0, 2;
77 0 0         $self->[2]{$key}= 1 unless $self->[2]{$key};
78 0           $self->[0]{$key}= shift;
79             }
80              
81             1;
82              
83             __END__
84              
85             =head1 NAME
86              
87             Egg::Plugin::Encode - Conversion function of character.
88              
89             =head1 SYNOPSIS
90              
91             use Egg qw/ Encode /;
92            
93             my $utf8= $e->utf_conv($text);
94             my $sjis= $e->sjis_conv($text);
95             my $euc = $e->euc_conv($text);
96              
97             =head1 DESCRIPTION
98              
99             Plugin that offers method of converting character-code.
100              
101             The character-code is converted with L<Jcode>.
102              
103             The supported character-code is 'euc', 'sjis', 'utf8'.
104              
105             Please make the 'create_encode' method in the project, and return the object that
106             does the code conversion from the method when converting it excluding L<Jcode>.
107              
108             sub create_encode {
109             AnyComvert->new;
110             }
111              
112             It sets it up so that all the input received with L<Egg::Request> is united by
113             the character-code when 'character_in' is defined by the configuration of Egg.
114              
115             If it wants to treat the code not supported by this plugin, the code conversion
116             can be done in that making the method in which '[code_name]_conv' in the project.
117             And, when the [code_name] is set to 'character_in', the input united by a target
118             code comes to be received.
119              
120             sub anyname_conv {
121             shift->encode->set(@_)->anyname;
122             }
123            
124             # Egg configuration.
125            
126             character_in => 'anyname',
127              
128             =head1 METHODS
129              
130             =head2 encode
131              
132             The object obtained by the 'create_encode' method is returned.
133              
134             my $conv_text= $e->encode->set(\$text)->utf8;
135              
136             =head2 create_encode
137              
138             The object to convert the character-code is returned.
139              
140             L<Jcode> is restored in default.
141              
142             If the object that treats the character-code is changed, this method is overwrited
143             as a controller etc.
144              
145             =head2 utf8_conv ([TEXT])
146              
147             The character-code is converted into utf8.
148              
149             my $utf8= $e->utf_conv(\$text);
150              
151             =head2 sjis_conv ([TEXT]);
152              
153             The character-code is converted into Shift_JIS.
154              
155             my $sjis= $e->sjis_conv(\$text);
156              
157             =head2 euc_conv ([TEXT]);
158              
159             The character-code is converted into EUC-JP.
160              
161             my $euc= $e->euc_conv(\$text);
162              
163             =head1 BUGS
164              
165             Jcode.pm is used and note the point that is always utf8 about the content, please
166             if you do not receive the conversion result when the character to be converted
167             into the method of *_ conv is passed by the SCALAR reference though it is not
168             a translation of bug.
169             This is because of being internally processed with utf8 in the specification of
170             Jcode.
171              
172             my $text= 'test'; # For shift_jis.
173             $e->euc_conv(\$text); # The content of $text is utf8.
174             $text= $e->euc_conv(\$text); # The content of $text is euc.
175            
176             $e->utf8_conv(\$text); # This is untouched utf8.
177              
178             Perhaps, I think that it is a peculiar problem when L<Jcode> operates as Wrapper
179             of L<Encode> module.
180              
181             =head1 SEE ALSO
182              
183             L<Egg::Release>,
184             L<Egg::Request>,
185             L<Jcode>,
186              
187             =head1 AUTHOR
188              
189             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
190              
191             =head1 COPYRIGHT AND LICENSE
192              
193             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
194              
195             This library is free software; you can redistribute it and/or modify
196             it under the same terms as Perl itself, either Perl version 5.8.6 or,
197             at your option, any later version of Perl 5 you may have available.
198              
199             =cut
200