| 1 | package encoding::source; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 7 | ▶ | use 5.009005; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 7 | ▶ | use Encode qw(find_encoding); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 7 | our $VERSION = 0.03; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 7 | our $SINGLETON = bless {}, __PACKAGE__; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub croak { | ||||||||||||||||||
|
Subroutine
croak: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 13 | 0 | require Carp; | |||||||||||||||||||
| 14 | 0 | Carp::croak(__PACKAGE__ . ": @_"); | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 7 | ▶ | my $LATIN1 = find_encoding('iso-8859-1') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | or croak("Can't load latin-1"); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 7 | ▶ | sub _find_encoding { | ||||||||||||||||||
|
Subroutine
_find_encoding: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 21 | 7 | my $name = shift; | |||||||||||||||||||
| 22 | 7 | ▶ | return $SINGLETON->{$name} // find_encoding($name); | ||||||||||||||||||
|
Condition33%1/3
$encoding::source::SINGLETON->{$name} // &find_encoding($name)
MC/DC0%0/2
$encoding::source::SINGLETON->{$name} // &find_encoding($name)
$encoding::source::SINGLETON->{$name}
&find_encoding($name)
Truth table33%1/3
A // B
A= $encoding::source::SINGLETON->{$name}B= &find_encoding($name)
| |||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 7 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.8
| |||||||||||||||||||||
| 26 | 7 | my ($class, $name) = @_; | |||||||||||||||||||
| 27 | 7 | my $enc = _find_encoding($name); | |||||||||||||||||||
| 28 | 7 | ▶ | if (!defined $enc) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 0 | croak("Unknown encoding '$name'"); | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | # canonicalize the encoding name | ||||||||||||||||||||
| 32 | 7 | $name = $enc->name; | |||||||||||||||||||
| 33 | # associate it to the currently compiled lexical unit | ||||||||||||||||||||
| 34 | 7 | $^H{$class} = $name; | |||||||||||||||||||
| 35 | # remember the Encode object for that encoding | ||||||||||||||||||||
| 36 | 7 | ▶ | $SINGLETON->{$name} //= $enc; | ||||||||||||||||||
|
Condition33%1/3
$encoding::source::SINGLETON->{$name} //= $enc
MC/DC0%0/2
$encoding::source::SINGLETON->{$name} //= $enc
$encoding::source::SINGLETON->{$name}
$enc
Truth table33%1/3
A //= B
A= $encoding::source::SINGLETON->{$name}B= $enc
| |||||||||||||||||||||
| 37 | # make sure to install our encoding handler | ||||||||||||||||||||
| 38 | 7 | ${^ENCODING} = $SINGLETON; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | sub unimport { | ||||||||||||||||||
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 42 | 0 | my $class = shift; | |||||||||||||||||||
| 43 | 0 | 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 | 0 | ▶ | sub name { | ||||||||||||||||||
|
Subroutine
name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 52 | 0 | ▶ | my $level = $_[1] // 0; | ||||||||||||||||||
|
Condition0%0/2
$_[1] // 0
MC/DC0%0/1
$_[1] // 0
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||
| 53 | 0 | my $hinthash = (caller($level))[10]; | |||||||||||||||||||
| 54 | 0 | return $hinthash->{"" . __PACKAGE__}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # the other methods are just forwarded to the appropriate | ||||||||||||||||||||
| 58 | # Encode object, retrieved in the $SINGLETON | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 7 | for my $method (qw(decode cat_decode)) { | |||||||||||||||||||
| 61 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 0 | ▶ | *$method = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=20.3
| |||||||||||||||||||||
| 63 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||
| 65 | 0 | my $name = $self->name(1); | |||||||||||||||||||
| 66 | 0 | ▶ | if ($name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | my $enc = $self->{$name}; | |||||||||||||||||||
| 68 | 0 | ▶ | if (!defined $enc) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | croak("Can't find compiled encoding for '$name'"); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | 0 | $enc->$method(@_); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||
| 74 | 0 | $LATIN1->$method(@_); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | 14 | }; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 7 | 1; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||