| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package encoding::source; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 7 |  |  | 7 |  | 74592 | use 5.009005; | 
|  | 7 |  |  |  |  | 27 |  | 
|  | 7 |  |  |  |  | 334 |  | 
| 4 | 7 |  |  | 7 |  | 36 | use strict; | 
|  | 7 |  |  |  |  | 12 |  | 
|  | 7 |  |  |  |  | 272 |  | 
| 5 | 7 |  |  | 7 |  | 38 | use warnings; | 
|  | 7 |  |  |  |  | 14 |  | 
|  | 7 |  |  |  |  | 252 |  | 
| 6 | 7 |  |  | 7 |  | 8003 | use Encode qw(find_encoding); | 
|  | 7 |  |  |  |  | 88248 |  | 
|  | 7 |  |  |  |  | 3266 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = 0.02; | 
| 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 |  | 18 | my $name = shift; | 
| 22 | 12 |  | 66 |  |  | 160 | return $SINGLETON->{$name} // find_encoding($name); | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub import { | 
| 26 | 12 |  |  | 12 |  | 110 | my ($class, $name) = @_; | 
| 27 | 12 |  |  |  |  | 27 | my $enc = _find_encoding($name); | 
| 28 | 12 | 50 |  |  |  | 42363 | if (!defined $enc) { | 
| 29 | 0 |  |  |  |  | 0 | croak("Unknown encoding '$name'"); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | # canonicalize the encoding name | 
| 32 | 12 |  |  |  |  | 111 | $name = $enc->name; | 
| 33 |  |  |  |  |  |  | # associate it to the currently compiled lexical unit | 
| 34 | 12 |  |  |  |  | 73 | $^H{$class} = $name; | 
| 35 |  |  |  |  |  |  | # remember the Encode object for that encoding | 
| 36 | 12 |  | 66 |  |  | 79 | $SINGLETON->{$name} //= $enc; | 
| 37 |  |  |  |  |  |  | # make sure to install our encoding handler | 
| 38 | 12 |  |  |  |  | 366 | ${^ENCODING} = $SINGLETON; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub unimport { | 
| 42 | 2 |  |  | 2 |  | 81 | my $class = shift; | 
| 43 | 2 |  |  |  |  | 53 | 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 | 3511 |  | 100 | 3511 | 0 | 7181 | my $level = $_[1] // 0; | 
| 53 | 3511 |  |  |  |  | 11835 | my $hinthash = (caller($level))[10]; | 
| 54 | 3511 |  |  |  |  | 8635 | 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 |  | 63 | no strict 'refs'; | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 255 |  | 
| 62 |  |  |  |  |  |  | *$method = sub { | 
| 63 | 7 |  |  | 7 |  | 31 | use strict; | 
|  | 7 |  |  |  |  | 9 |  | 
|  | 7 |  |  |  |  | 1015 |  | 
| 64 | 3501 |  |  | 3501 |  | 163174 | my $self = shift; | 
| 65 | 3501 |  |  |  |  | 7372 | my $name = $self->name(1); | 
| 66 | 3501 | 100 |  |  |  | 6089 | if ($name) { | 
| 67 | 443 |  |  |  |  | 624 | my $enc = $self->{$name}; | 
| 68 | 443 | 50 |  |  |  | 871 | if (!defined $enc) { | 
| 69 | 0 |  |  |  |  | 0 | croak("Can't find compiled encoding for '$name'"); | 
| 70 |  |  |  |  |  |  | } | 
| 71 | 443 |  |  |  |  | 12494 | $enc->$method(@_); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | else { | 
| 74 | 3058 |  |  |  |  | 306643 | $LATIN1->$method(@_); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | }; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | 1; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | __END__ |