File Coverage

blib/lib/encoding/source.pm
Criterion Covered Total %
statement 38 42 90.4
branch 4 6 66.6
condition 6 8 75.0
subroutine 11 12 91.6
pod 0 2 0.0
total 59 70 84.2


line stmt bran cond sub pod time code
1             package encoding::source;
2              
3 7     7   60554 use 5.009005;
  7         21  
4 7     7   33 use strict;
  7         11  
  7         142  
5 7     7   31 use warnings;
  7         21  
  7         205  
6 7     7   6077 use Encode qw(find_encoding);
  7         79466  
  7         2722  
7              
8             our $VERSION = 0.03;
9              
10             our $SINGLETON = bless {}, __PACKAGE__;
11              
12             sub croak {
13 0     0 0 0 require Carp;
14 0         0 Carp::croak(__PACKAGE__ . ": @_");
15             }
16              
17             my $LATIN1 = find_encoding('iso-8859-1')
18             or croak("Can't load latin-1");
19              
20             sub _find_encoding {
21 12     12   24 my $name = shift;
22 12   66     189 return $SINGLETON->{$name} // find_encoding($name);
23             }
24              
25             sub import {
26 12     12   123 my ($class, $name) = @_;
27 12         30 my $enc = _find_encoding($name);
28 12 50       15560905 if (!defined $enc) {
29 0         0 croak("Unknown encoding '$name'");
30             }
31             # canonicalize the encoding name
32 12         125 $name = $enc->name;
33             # associate it to the currently compiled lexical unit
34 12         87 $^H{$class} = $name;
35             # remember the Encode object for that encoding
36 12   66     86 $SINGLETON->{$name} //= $enc;
37             # make sure to install our encoding handler
38 12         1295 ${^ENCODING} = $SINGLETON;
39             }
40              
41             sub unimport {
42 2     2   131 my $class = shift;
43 2         69 undef $^H{$class};
44             }
45              
46             # now, the three methods called by the core on ${^ENCODING}
47              
48             # returns the name of the encoding which is in effect in the
49             # caller's lexical unit
50              
51             sub name {
52 34999   100 34999 0 68905 my $level = $_[1] // 0;
53 34999         107339 my $hinthash = (caller($level))[10];
54 34999         77835 return $hinthash->{"" . __PACKAGE__};
55             }
56              
57             # the other methods are just forwarded to the appropriate
58             # Encode object, retrieved in the $SINGLETON
59              
60             for my $method (qw(decode cat_decode)) {
61 7     7   54 no strict 'refs';
  7         14  
  7         248  
62             *$method = sub {
63 7     7   36 use strict;
  7         18  
  7         887  
64 34989     34989   379530 my $self = shift;
65 34989         60923 my $name = $self->name(1);
66 34989 100       58255 if ($name) {
67 436         680 my $enc = $self->{$name};
68 436 50       867 if (!defined $enc) {
69 0         0 croak("Can't find compiled encoding for '$name'");
70             }
71 436         13536 $enc->$method(@_);
72             }
73             else {
74 34553         750797 $LATIN1->$method(@_);
75             }
76             };
77             }
78              
79             1;
80              
81             __END__