| 1 | #======================================================================== | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Badger::Codec::Chain | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Codec for encoding/decoding data via a chain of other codecs. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # AUTHOR | ||||||||||||||||||||||||||||
| 9 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | #======================================================================== | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Badger::Codec::Chain; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use Badger::Codecs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | use Badger::Class | ||||||||||||||||||||||||||||
| 17 | 15 | version => 0.01, | |||||||||||||||||||||||||||
| 18 | debug => 0, | ||||||||||||||||||||||||||||
| 19 | base => 'Badger::Codec', | ||||||||||||||||||||||||||||
| 20 | constants => 'ARRAY', | ||||||||||||||||||||||||||||
| 21 | constant => { | ||||||||||||||||||||||||||||
| 22 | CODECS => 'Badger::Codecs', | ||||||||||||||||||||||||||||
| 23 | CHAIN => __PACKAGE__, | ||||||||||||||||||||||||||||
| 24 | CHAINED => qr/\s*\+\s*/, | ||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||
| 26 | exports => { | ||||||||||||||||||||||||||||
| 27 | any => 'CHAIN CHAINED' | ||||||||||||||||||||||||||||
| 28 | 15 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub new { | ||||||||||||||||||||||||||||
| 32 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | my $chain = @_ == 1 ? shift : [ @_ ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # single argument can be a text string or array ref | ||||||||||||||||||||||||||||
| 36 | # each argument in an array can be a codec ref or codec name/chain | ||||||||||||||||||||||||||||
| 37 | # all codec names must be upgraded to codec objects | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | $chain = [ $chain ] unless ref $chain eq ARRAY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | $chain = [ map { ref $_ ? $_ : split(CHAINED, $_) } @$chain ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | $chain = [ map { ref $_ ? $_ : CODECS->codec($_) } @$chain ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | $class->debug("chaining codecs: ", join(' + ', @$chain), "\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | bless { | |||||||||||||||||||||||||||
| 45 | chain => $chain, | ||||||||||||||||||||||||||||
| 46 | }, $class; | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub encode { | ||||||||||||||||||||||||||||
| 50 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 3 | my $data = shift; | |||||||||||||||||||||||||||
| 52 | 3 | foreach my $codec (@{ $self->{ chain } }) { | |||||||||||||||||||||||||||
| 53 | 6 | $data = $codec->encode($data); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 3 | return $data; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub decode { | ||||||||||||||||||||||||||||
| 59 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 3 | my $data = shift; | |||||||||||||||||||||||||||
| 61 | 3 | foreach my $codec (reverse @{ $self->{ chain } }) { | |||||||||||||||||||||||||||
| 62 | 6 | $data = $codec->decode($data); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 3 | return $data; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub encoder { | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
encoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | return $self->coder( | ||||||||||||||||||||||||||||
| 70 | 4 | map { $_->encoder } | |||||||||||||||||||||||||||
| 71 | 2 | @{ $self->{ chain } } | |||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub decoder { | ||||||||||||||||||||||||||||
| 76 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
decoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | return $self->coder( | ||||||||||||||||||||||||||||
| 78 | 4 | reverse map { $_->decoder } | |||||||||||||||||||||||||||
| 79 | 2 | @{ $self->{ chain } } | |||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub coder { | ||||||||||||||||||||||||||||
| 84 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
coder: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 4 | ▶ | my $coders = @_ && ref $_[0] eq ARRAY ? shift : [@_]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0] eq "ARRAY"
MC/DC0%0/2
@_ and ref $_[0] eq "ARRAY"
@_
ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 86 | return sub { | ||||||||||||||||||||||||||||
| 87 | 4 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 88 | 4 | foreach my $coder (@$coders) { | |||||||||||||||||||||||||||
| 89 | 8 | $data = $coder->($data); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 4 | return $data; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 4 | } | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 15 | 1; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||