2 # $Id: Encoder.pm,v 0.1 2002/04/08 02:35:10 dankogai Exp $
6 our $VERSION = do { my @r = (q$Revision: 0.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
9 our @ISA = qw(Exporter);
10 # Public, encouraged API is exported by default
17 use Encode qw(encode decode find_encoding from_to);
21 my ($class, $data, $encname) = @_;
23 my $obj = find_encoding($encname)
24 or croak __PACKAGE__, ": unknown encoding: $encname";
27 encoding => $obj->name,
29 bless $self => $class;
32 sub encoder{ shift->new(@_) }
35 my ($self, $data) = shift;
36 defined $data and $self->{data} = $data;
41 my ($self, $encname) = @_;
43 my $obj = find_encoding($encname)
44 or confess __PACKAGE__, ": unknown encoding: $encname";
45 $self->{encoding} = $obj->name;
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;
65 q("") => sub { $_[0]->{data} },
66 q(0+) => sub { use bytes (); bytes::length($_[0]->{data}) },
75 Encode::Encoder -- Object Oriented Encoder
80 # Encode::encode("ISO-8859-1", $data);
81 Encoder->new($data)->iso_8859_1; # OOP way
83 encoder($data)->iso_8859_1;
85 encoder($data)->iso_8859_1->base64; # provided base64() is defined
87 print encoder($utf8)->latin1 # prints the string in latin1
89 encoder("\x{abcd}\x{ef}g") == 6; # true. bytes::length($data)
93 B<Encode::Encoder> allows you to use Encode via OOP style. This is
94 not only more intuitive than functional approach, but also handier
95 when you want to stack encodings. Suppose you want your UTF-8 string
96 converted to Latin1 then Base64, you can simply say
98 my $base64 = encoder($utf8)->latin1->base64;
102 my $latin1 = encode("latin1", $utf8);
104 or lazier and convolted
106 my $base64 = encode_base64(encode("latin1", $utf8));
110 Here is how to use this module.
116 There are at least two instance variable stored in hash reference,
117 {data} and {encoding}.
121 When there is no method, it takes the method name as the name of
122 encoding and encode instance I<data> with I<encoding>. If successful,
123 instance I<encoding> is set accordingly.
127 This module is desined to work with L<Encode::Encoding>.
128 To make the Base64 transcorder example above really work, you should
129 write a module like this.
131 package Encode::Base64;
132 use base 'Encode::Encoding';
133 __PACKAGE->Define('base64');
136 my ($obj, $data) = @_;
137 return encode_base64($data);
140 my ($obj, $data) = @_;
141 return decode_base64($data);
146 And your caller module should be like this;
152 =head2 operator overloading
154 This module overloads two operators, stringify ("") and numify (0+).
156 Stringify dumps the data therein.
158 Numify returns the number of bytes therein.
160 They come in handy when you want to print or find the size of data.
164 =head2 Predefined Methods
166 This module predefines the methods below;
170 =item $e = Encode::Encoder-E<gt>new([$data, $encoding]);
172 returns the encoder object. Its data is initialized with $data if
173 there, and its encoding is set to $encoding if there.
177 is an alias of Encode::Encoder-E<gt>new(). This one is exported for
180 =item $e-E<gt>data($data)
182 sets instance data to $data.
184 =item $e-E<gt>encoding($encoding)
186 sets instance encoding to $encoding