Re: lib/sort.t failure -- real PATCH enclosed
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / Encoder.pm
CommitLineData
c731e18e 1#
2# $Id: Encoder.pm,v 0.1 2002/04/08 02:35:10 dankogai Exp $
3#
4package Encoder;
5use strict;
6our $VERSION = do { my @r = (q$Revision: 0.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
7
8require Exporter;
9our @ISA = qw(Exporter);
10# Public, encouraged API is exported by default
11our @EXPORT = qw (
12 encoder
13);
14
15our $AUTOLOAD;
16our $DEBUG = 0;
17use Encode qw(encode decode find_encoding from_to);
18use Carp;
19
20sub new{
21 my ($class, $data, $encname) = @_;
22 $encname ||= 'utf8';
23 my $obj = find_encoding($encname)
24 or croak __PACKAGE__, ": unknown encoding: $encname";
25 my $self = {
26 data => $data,
27 encoding => $obj->name,
28 };
29 bless $self => $class;
30}
31
32sub encoder{ shift->new(@_) }
33
34sub data{
35 my ($self, $data) = shift;
36 defined $data and $self->{data} = $data;
37 return $self;
38}
39
40sub encoding{
41 my ($self, $encname) = @_;
42 if ($encname){
43 my $obj = find_encoding($encname)
44 or confess __PACKAGE__, ": unknown encoding: $encname";
45 $self->{encoding} = $obj->name;
46 }
47 $self;
48}
49
50sub AUTOLOAD {
51 my $self = shift;
52 my $type = ref($self)
53 or confess "$self is not an object";
54 my $myname = $AUTOLOAD;
55 $myname =~ s/.*://; # strip fully-qualified portion
56 my $obj = find_encoding($myname)
57 or confess __PACKAGE__, ": unknown encoding: $myname";
58 $DEBUG and warn $self->{encoding}, " => ", $obj->name;
59 from_to($self->{data}, $self->{encoding}, $obj->name, 1);
60 $self->{encoding} = $obj->name;
61 return $self;
62}
63
64use overload
65 q("") => sub { $_[0]->{data} },
66 q(0+) => sub { use bytes (); bytes::length($_[0]->{data}) },
67 fallback => 1,
68 ;
69
701;
71__END__
72
73=head1 NAME
74
75Encode::Encoder -- Object Oriented Encoder
76
77=head1 SYNOPSIS
78
79 use Encode::Encoder;
80 # Encode::encode("ISO-8859-1", $data);
81 Encoder->new($data)->iso_8859_1; # OOP way
82 # shortcut
83 encoder($data)->iso_8859_1;
84 # you can stack them!
85 encoder($data)->iso_8859_1->base64; # provided base64() is defined
86 # stringified
87 print encoder($utf8)->latin1 # prints the string in latin1
88 # numified
89 encoder("\x{abcd}\x{ef}g") == 6; # true. bytes::length($data)
90
91=head1 ABSTRACT
92
93B<Encode::Encoder> allows you to use Encode via OOP style. This is
94not only more intuitive than functional approach, but also handier
95when you want to stack encodings. Suppose you want your UTF-8 string
96converted to Latin1 then Base64, you can simply say
97
98 my $base64 = encoder($utf8)->latin1->base64;
99
100instead of
101
102 my $latin1 = encode("latin1", $utf8);
103
104or lazier and convolted
105
106 my $base64 = encode_base64(encode("latin1", $utf8));
107
108=head1 Description
109
110Here is how to use this module.
111
112=over 4
113
114=item *
115
116There are at least two instance variable stored in hash reference,
117{data} and {encoding}.
118
119=item *
120
121When there is no method, it takes the method name as the name of
122encoding and encode instance I<data> with I<encoding>. If successful,
123instance I<encoding> is set accordingly.
124
125=item *
126
127This module is desined to work with L<Encode::Encoding>.
128To make the Base64 transcorder example above really work, you should
129write a module like this.
130
131 package Encode::Base64;
132 use base 'Encode::Encoding';
133 __PACKAGE->Define('base64');
134 use MIME::Base64;
135 sub encode{
136 my ($obj, $data) = @_;
137 return encode_base64($data);
138 }
139 sub decode{
140 my ($obj, $data) = @_;
141 return decode_base64($data);
142 }
143 1;
144 __END__
145
146And your caller module should be like this;
147
148 use Encode::Encoder;
149 use Encode::Base64;
150 # and be creative.
151
152=head2 operator overloading
153
154This module overloads two operators, stringify ("") and numify (0+).
155
156Stringify dumps the data therein.
157
158Numify returns the number of bytes therein.
159
160They come in handy when you want to print or find the size of data.
161
162=back
163
164=head2 Predefined Methods
165
166This module predefines the methods below;
167
168=over 4
169
170=item $e = Encode::Encoder-E<gt>new([$data, $encoding]);
171
172returns the encoder object. Its data is initialized with $data if
173there, and its encoding is set to $encoding if there.
174
175=item encoder()
176
177is an alias of Encode::Encoder-E<gt>new(). This one is exported for
178convenience.
179
180=item $e-E<gt>data($data)
181
182sets instance data to $data.
183
184=item $e-E<gt>encoding($encoding)
185
186sets instance encoding to $encoding
187
188=back
189
190=head1 SEE ALSO
191
192L<Encode>
193L<Encode::Encoding>
194
195=cut