| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
10
|
|
|
|
|
|
|
# |
|
11
|
|
|
|
|
|
|
#======================================================================== |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Badger::Codec::Chain; |
|
14
|
|
|
|
|
|
|
|
|
15
|
15
|
|
|
15
|
|
107
|
use Badger::Codecs; |
|
|
15
|
|
|
|
|
30
|
|
|
|
15
|
|
|
|
|
1239
|
|
|
16
|
|
|
|
|
|
|
use Badger::Class |
|
17
|
15
|
|
|
|
|
268
|
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
|
|
|
15
|
|
951
|
}; |
|
|
15
|
|
|
|
|
28
|
|
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
|
32
|
4
|
|
|
4
|
1
|
7
|
my $class = shift; |
|
33
|
4
|
50
|
|
|
|
12
|
my $chain = @_ == 1 ? shift : [ @_ ]; |
|
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
|
50
|
|
|
|
14
|
$chain = [ $chain ] unless ref $chain eq ARRAY; |
|
39
|
4
|
50
|
|
|
|
23
|
$chain = [ map { ref $_ ? $_ : split(CHAINED, $_) } @$chain ]; |
|
|
4
|
|
|
|
|
29
|
|
|
40
|
4
|
50
|
|
|
|
13
|
$chain = [ map { ref $_ ? $_ : CODECS->codec($_) } @$chain ]; |
|
|
8
|
|
|
|
|
27
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
4
|
50
|
|
|
|
30
|
$class->debug("chaining codecs: ", join(' + ', @$chain), "\n") if $DEBUG; |
|
43
|
|
|
|
|
|
|
|
|
44
|
4
|
|
|
|
|
18
|
bless { |
|
45
|
|
|
|
|
|
|
chain => $chain, |
|
46
|
|
|
|
|
|
|
}, $class; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub encode { |
|
50
|
3
|
|
|
3
|
1
|
12
|
my $self = shift; |
|
51
|
3
|
|
|
|
|
4
|
my $data = shift; |
|
52
|
3
|
|
|
|
|
4
|
foreach my $codec (@{ $self->{ chain } }) { |
|
|
3
|
|
|
|
|
13
|
|
|
53
|
6
|
|
|
|
|
97
|
$data = $codec->encode($data); |
|
54
|
|
|
|
|
|
|
} |
|
55
|
3
|
|
|
|
|
11
|
return $data; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub decode { |
|
59
|
3
|
|
|
3
|
1
|
8
|
my $self = shift; |
|
60
|
3
|
|
|
|
|
5
|
my $data = shift; |
|
61
|
3
|
|
|
|
|
4
|
foreach my $codec (reverse @{ $self->{ chain } }) { |
|
|
3
|
|
|
|
|
7
|
|
|
62
|
6
|
|
|
|
|
14
|
$data = $codec->decode($data); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
3
|
|
|
|
|
67
|
return $data; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub encoder { |
|
68
|
2
|
|
|
2
|
1
|
6
|
my $self = shift; |
|
69
|
|
|
|
|
|
|
return $self->coder( |
|
70
|
4
|
|
|
|
|
10
|
map { $_->encoder } |
|
71
|
2
|
|
|
|
|
4
|
@{ $self->{ chain } } |
|
|
2
|
|
|
|
|
10
|
|
|
72
|
|
|
|
|
|
|
); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub decoder { |
|
76
|
2
|
|
|
2
|
1
|
3
|
my $self = shift; |
|
77
|
|
|
|
|
|
|
return $self->coder( |
|
78
|
4
|
|
|
|
|
8
|
reverse map { $_->decoder } |
|
79
|
2
|
|
|
|
|
6
|
@{ $self->{ chain } } |
|
|
2
|
|
|
|
|
5
|
|
|
80
|
|
|
|
|
|
|
); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub coder { |
|
84
|
4
|
|
|
4
|
1
|
6
|
my $self = shift; |
|
85
|
4
|
50
|
33
|
|
|
33
|
my $coders = @_ && ref $_[0] eq ARRAY ? shift : [@_]; |
|
86
|
|
|
|
|
|
|
return sub { |
|
87
|
4
|
|
|
4
|
|
12
|
my $data = shift; |
|
88
|
4
|
|
|
|
|
8
|
foreach my $coder (@$coders) { |
|
89
|
8
|
|
|
|
|
125
|
$data = $coder->($data); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
4
|
|
|
|
|
62
|
return $data; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
4
|
|
|
|
|
18
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |