| 1 | # | ||||||||||||||||||||
| 2 | # $Id: Encoder.pm,v 2.04 2026/10/26 01:42:18 dankogai Exp $ | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | package Encode::Encoder; | ||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | our $VERSION = '2.04'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | require Exporter; | |||||||||||||||||||
| 10 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 11 | 2 | our @EXPORT_OK = qw ( encoder ); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | our $AUTOLOAD; | |||||||||||||||||||
| 14 | 2 | ▶ | use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | ▶ | use Encode qw(encode decode find_encoding from_to); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 514 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 19 | 514 | my ( $class, $data, $encname ) = @_; | |||||||||||||||||||
| 20 | 514 | ▶ | unless ($encname) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 513 | ▶ | $encname = Encode::is_utf8($data) ? 'utf8' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | else { | ||||||||||||||||||||
| 24 | 1 | ▶ | my $obj = find_encoding($encname) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | or croak __PACKAGE__, ": unknown encoding: $encname"; | ||||||||||||||||||||
| 26 | 1 | $encname = $obj->name; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | 514 | my $self = { | |||||||||||||||||||
| 29 | data => $data, | ||||||||||||||||||||
| 30 | encoding => $encname, | ||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||
| 32 | 514 | bless $self => $class; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 514 | ▶ | sub encoder { __PACKAGE__->new(@_) } | ||||||||||||||||||
|
Subroutine
encoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 2 | ▶ | sub data { | ||||||||||||||||||
|
Subroutine
data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 38 | 2 | my ( $self, $data ) = @_; | |||||||||||||||||||
| 39 | 2 | ▶ | if ( defined $data ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 1 | $self->{data} = $data; | |||||||||||||||||||
| 41 | 1 | return $data; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||
| 44 | 1 | return $self->{data}; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 2 | ▶ | sub encoding { | ||||||||||||||||||
|
Subroutine
encoding: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 49 | 2 | my ( $self, $encname ) = @_; | |||||||||||||||||||
| 50 | 2 | ▶ | if ($encname) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 1 | ▶ | my $obj = find_encoding($encname) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | or confess __PACKAGE__, ": unknown encoding: $encname"; | ||||||||||||||||||||
| 53 | 1 | $self->{encoding} = $obj->name; | |||||||||||||||||||
| 54 | 1 | return $self; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||
| 57 | 1 | return $self->{encoding}; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 256 | ▶ | sub bytes { | ||||||||||||||||||
|
Subroutine
bytes: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||
| 62 | 256 | my ( $self, $encname ) = @_; | |||||||||||||||||||
| 63 | 256 | ▶ | $encname ||= $self->{encoding}; | ||||||||||||||||||
|
Condition33%1/3
$encname ||= $self->{"encoding"}
MC/DC0%0/2
$encname ||= $self->{"encoding"}
$encname
$self->{"encoding"}
Truth table33%1/3
A ||= B
A= $encnameB= $self->{"encoding"}
| |||||||||||||||||||||
| 64 | 256 | ▶ | my $obj = find_encoding($encname) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | or confess __PACKAGE__, ": unknown encoding: $encname"; | ||||||||||||||||||||
| 66 | 256 | $self->{data} = $obj->decode( $self->{data}, 1 ); | |||||||||||||||||||
| 67 | 256 | $self->{encoding} = ''; | |||||||||||||||||||
| 68 | 256 | return $self; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 514 | ▶ | sub DESTROY { # defined so it won't autoload. | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 72 | 514 | DEBUG and warn shift; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 257 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=14.4
| |||||||||||||||||||||
| 76 | 257 | my $self = shift; | |||||||||||||||||||
| 77 | 257 | ▶ | my $type = ref($self) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | or confess "$self is not an object"; | ||||||||||||||||||||
| 79 | 257 | my $myname = $AUTOLOAD; | |||||||||||||||||||
| 80 | 257 | $myname =~ s/.*://; # strip fully-qualified portion | |||||||||||||||||||
| 81 | 257 | ▶ | my $obj = find_encoding($myname) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | or confess __PACKAGE__, ": unknown encoding: $myname"; | ||||||||||||||||||||
| 83 | 257 | DEBUG and warn $self->{encoding}, " => ", $obj->name; | |||||||||||||||||||
| 84 | 257 | ▶ | if ( $self->{encoding} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | from_to( $self->{data}, $self->{encoding}, $obj->name, 1 ); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||
| 88 | 257 | $self->{data} = $obj->encode( $self->{data}, 1 ); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 257 | $self->{encoding} = $obj->name; | |||||||||||||||||||
| 91 | 257 | return $self; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | use overload | ||||||||||||||||||||
| 95 | 512 | ▶ | q("") => sub { $_[0]->{data} }, | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 96 | 2 | ▶ | q(0+) => sub { use bytes(); bytes::length( $_[0]->{data} ) }, | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 97 | 2 | fallback => 1, | |||||||||||||||||||
| 98 | 2 | ; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 2 | 1; | |||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||