File Coverage

blib/lib/Data/Decode.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::Decode;
2 7     7   248032 use Moose;
  0            
  0            
3             use Moose::Util::TypeConstraints;
4             use namespace::clean -except => qw(meta);
5             use Data::Decode::Exception;
6             use Data::Decode::Types;
7              
8             our $VERSION = '0.00007_01';
9              
10             has decoder => (
11             is => 'ro',
12             isa => 'Data::Decode::Decoder',
13             required => 1,
14             coerce => 1,
15             );
16              
17             sub import {
18             my ( $self, @modules ) = @_;
19              
20             foreach my $class (@modules) {
21             if ($class !~ s/^\+//) {
22             $class = "Data::Decode::$class";
23             Class::MOP::load_class($class);
24             }
25             }
26             }
27              
28             sub decode {
29             my ($self, $data, $hints) = @_;
30              
31             return () unless defined $data;
32             $hints ||= {};
33              
34             my $ret = eval {
35             $self->decoder->decode($self, $data, $hints);
36             };
37             my $e;
38             if ($e = Data::Decode::Exception::Deferred->caught() ) {
39             # Just deferred. return ()
40             return ();
41             } elsif ( $e = Exception::Class->caught() ) {
42             # Oh, this we re-throw
43             eval { $e->isa('Data::Decode::Exception') } ?
44             $e->rethrow : die $e;
45             }
46             return $ret;
47             }
48              
49             1;
50              
51             __END__
52              
53             =head1 NAME
54              
55             Data::Decode - Pluggable Data Decoder
56              
57             =head1 SYNOPSIS
58              
59             # simple usage (you probably won't use this form much)
60             use Data::Decode qw( Encode::Guess );
61              
62             my $decoder = Data::Decode->new(
63             decoder => Data::Decode::Encode::Gues->new()
64             );
65             $decoder->decode($data);
66              
67             # cascading several decoders
68             use Data::Decode
69             qw( HTTP::Response Encode::Guess );
70              
71             my $decoder = Data::Decode->new(
72             decoder => [
73             Data::Decode::Encode::HTTP::Response->new(),
74             Data::Decode::Encode::Guess->new(),
75             ]
76             );
77              
78             my $res = LWP::UserAgent->new->get("http://whatever.example.com");
79              
80             my $decoded = $decoder->decode($res->content, { response => $res });
81              
82             =head1 DESCRIPTION
83              
84             Data::Decode implements a pluggable "decoder". The main aim is to provide
85             a uniform interface to decode a given data while allowing the actual
86             algorithm being used to be changed depending on your needs..
87              
88             For now this is aimed at decoding miscellaneous text to perl's internal
89             unicode encoding, but should be able to handle anything if you give it a
90             proper plugin
91              
92             =head1 DECODING TO UNICODE
93              
94             Japanese, which is the language that I mainly deal with, has an annoying
95             property: It can come in at least 4 different flavors (utf-8, shift-jis,
96             euc-jp and iso-2022-jp). Even worse, vendors may have more vendor-specific
97             symbols, such as the pictograms in mobile phones.
98              
99             Ways to decode these strings into unicode varies between each environment
100             and application.
101              
102             Many modules require that the strings be normalized to unicode, but they
103             all handle this normalization process differently, which is, well, not exactly
104             an optimal solution.
105              
106             Data::Decode provides a uniform interface to this problem, and a few common
107             ways decoding is handled. The actual decoding strategies are separated out
108             from the surface interface, so other users who find a particular strategy to
109             decode strings can then upload their way to CPAN, and everyone can benefit
110             from it.
111              
112             =head1 CASCADING
113              
114             Data::Decode comes with a simple chaining functionality. You can take as many
115             decoders as you want, and you can stack them on top of each other. To enable
116             this feature, just provide an array as the decoder, instead of a single object.
117              
118             =head1 METHODS
119              
120             =head2 new
121              
122             Instantiates a new Data::Decode object.
123              
124             =over 4
125              
126             =item decoder
127              
128             Required. Takes in the object that encapsulates the actual decoding logic.
129              
130             (WARNING: Subject to change - we may require an object that implements a role
131             instead of just a function in the future. Beware!) The object must have a
132             method named "decode", which takes in a reference to the Data::Decode object
133             and a string to be decoded. An optional third parameter may be provided to
134             specify any hints that could be used to figure out what to do.
135              
136             # a decode() method
137             sub decode {
138             my ($self, $decoder, $string, $hints) = @_;
139             # $decoder = Data::Decode object
140             # $string = a scalar to be decoded
141             # $hints = a hashref of hints
142             }
143              
144             You may also specify the class names of the decoders -- in that case, an
145             argument-less new() will be called upon the class name to instantiate the
146             decoder.
147              
148             If you provide a list of decoders, Data::Decode::Chain will automatically be
149             set for you.
150              
151             my $decoder = Data::Decode->new(
152             decoder => [ # This will turn into a Data::Decode::Chain object
153             Decoder1->new(),
154             Decoder2->new(),
155             Decoder3->new(),
156             ...
157             ]
158             );
159              
160             =back
161              
162             =head2 decode
163              
164             Decodes a string. Takes in a string, and a hashref of hints to be used
165             for decoding. The meaning or the usage of the hints may differ between
166             the actual underlying decoders.
167              
168             =head2 decoder
169              
170             Get the underlying decoder object.
171              
172             =head1 AUTHOR
173              
174             Daisuke Maki E<lt>daisuke@endeworks.jpE<gt>
175              
176             =head1 LICENSE
177              
178             This program is free software; you can redistribute it and/or modify it
179             under the same terms as Perl itself.
180              
181             See http://www.perl.com/perl/misc/Artistic.html
182              
183             =cut