File Coverage

blib/lib/Encode/Encoder.pm
Criterion Covered Total %
statement 59 60 98.3
branch 13 20 65.0
condition 1 3 33.3
subroutine 16 16 100.0
pod 5 5 100.0
total 94 104 90.3


line stmt bran cond sub pod time code
1             #
2             # $Id: Encoder.pm,v 2.3 2013/09/14 07:51:59 dankogai Exp $
3             #
4             package Encode::Encoder;
5 2     2   22464 use strict;
  2         8  
  2         76  
6 2     2   18 use warnings;
  2         8  
  2         326  
7             our $VERSION = do { my @r = ( q$Revision: 2.3 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
8              
9             require Exporter;
10             our @ISA = qw(Exporter);
11             our @EXPORT_OK = qw ( encoder );
12              
13             our $AUTOLOAD;
14 2     2   18 use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG};
  2         6  
  2         205  
15 2     2   557 use Encode qw(encode decode find_encoding from_to);
  2         5  
  2         193  
16 2     2   17 use Carp;
  2         6  
  2         1694  
17              
18             sub new {
19 514     514 1 1369 my ( $class, $data, $encname ) = @_;
20 514 100       1573 unless ($encname) {
21 513 50       1798 $encname = Encode::is_utf8($data) ? 'utf8' : '';
22             }
23             else {
24 1 50       7 my $obj = find_encoding($encname)
25             or croak __PACKAGE__, ": unknown encoding: $encname";
26 1         14 $encname = $obj->name;
27             }
28 514         1973 my $self = {
29             data => $data,
30             encoding => $encname,
31             };
32 514         2677 bless $self => $class;
33             }
34              
35 514     514 1 4430 sub encoder { __PACKAGE__->new(@_) }
36              
37             sub data {
38 2     2 1 9 my ( $self, $data ) = @_;
39 2 100       7 if ( defined $data ) {
40 1         33 $self->{data} = $data;
41 1         8 return $data;
42             }
43             else {
44 1         6 return $self->{data};
45             }
46             }
47              
48             sub encoding {
49 2     2 1 6 my ( $self, $encname ) = @_;
50 2 100       7 if ($encname) {
51 1 50       4 my $obj = find_encoding($encname)
52             or confess __PACKAGE__, ": unknown encoding: $encname";
53 1         9 $self->{encoding} = $obj->name;
54 1         7 return $self;
55             }
56             else {
57 1         6 return $self->{encoding};
58             }
59             }
60              
61             sub bytes {
62 256     256 1 600 my ( $self, $encname ) = @_;
63 256   33     698 $encname ||= $self->{encoding};
64 256 50       830 my $obj = find_encoding($encname)
65             or confess __PACKAGE__, ": unknown encoding: $encname";
66 256         1050 $self->{data} = $obj->decode( $self->{data}, 1 );
67 256         2113 $self->{encoding} = '';
68 256         868 return $self;
69             }
70              
71             sub DESTROY { # defined so it won't autoload.
72 514     514   135978 DEBUG and warn shift;
73             }
74              
75             sub AUTOLOAD {
76 257     257   569 my $self = shift;
77 257 50       869 my $type = ref($self)
78             or confess "$self is not an object";
79 257         528 my $myname = $AUTOLOAD;
80 257         1349 $myname =~ s/.*://; # strip fully-qualified portion
81 257 50       930 my $obj = find_encoding($myname)
82             or confess __PACKAGE__, ": unknown encoding: $myname";
83 257         523 DEBUG and warn $self->{encoding}, " => ", $obj->name;
84 257 50       835 if ( $self->{encoding} ) {
85 0         0 from_to( $self->{data}, $self->{encoding}, $obj->name, 1 );
86             }
87             else {
88 257         1115 $self->{data} = $obj->encode( $self->{data}, 1 );
89             }
90 257         2398 $self->{encoding} = $obj->name;
91 257         1018 return $self;
92             }
93              
94             use overload
95 512     512   66051 q("") => sub { $_[0]->{data} },
96 2     2   20 q(0+) => sub { use bytes(); bytes::length( $_[0]->{data} ) },
  2     1   6  
  2         130  
  1         43  
97 2         26 fallback => 1,
98 2     2   2853 ;
  2         2517  
99              
100             1;
101             __END__