File Coverage

lib/Badger/Codec/Encoding.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 4 4 100.0
total 36 36 100.0


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Codec::Encoding
4             #
5             # DESCRIPTION
6             # A codec wrapper for Encode that binds a specific encoding with it.
7             #
8             # AUTHOR
9             # Andy Wardley
10             #
11             #========================================================================
12              
13             package Badger::Codec::Encoding;
14              
15             use Badger::Class
16 3         29 version => 0.01,
17             debug => 0,
18             base => 'Badger::Codec',
19             import => 'class',
20             constants => 'PKG',
21             constant => {
22             encoding => 'ASCII',
23 3     3   2549 };
  3         5  
24              
25 3     3   1661 use Encode qw();
  3         40210  
  3         110  
26 3     3   37 use bytes;
  3         6  
  3         13  
27              
28             sub encode {
29             # replace $self with $self->encoding
30 1     1 1 20 unshift(@_, shift->encoding);
31             # you don't need to turn away, this isn't your average GOTO - it's a
32             # special kind of subroutine call that Larry called goto for a laugh
33 1         14 goto &Encode::encode;
34             }
35              
36             sub decode {
37             # as above: monkey with the args so the goto works as expected
38 1     1 1 9 unshift(@_, shift->encoding);
39 1         8 goto &Encode::decode;
40             }
41              
42             sub encoder {
43 3     3 1 7 my $self = shift;
44 3         12 my $enc = $self->encoding;
45             return sub {
46 1     1   49 unshift(@_, $enc);
47 1         4 goto &Encode::encode
48 3         12 };
49             }
50              
51             sub decoder {
52 3     3 1 5 my $self = shift;
53 3         7 my $enc = $self->encoding;
54             return sub {
55 1     1   21 unshift(@_, $enc);
56 1         4 goto &Encode::decode
57 3         12 };
58             }
59              
60              
61             #-----------------------------------------------------------------------
62             # generate subclasses for various UTF encodings
63             #-----------------------------------------------------------------------
64              
65             my $base = __PACKAGE__;
66             my @encs = qw( utf8 UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE );
67              
68             for my $enc (@encs) {
69             my $name = $enc;
70             $name =~ s/\W//g;
71              
72             # fetch a Badger::Class object for the subclass so we can define
73             # a base class and set the constant encoding() method
74             my $subclass = class($base.PKG.$name);
75             $subclass->base($base);
76             $subclass->constant( encoding => $enc );
77            
78             $base->debug("$name => $enc => ", $subclass->name->encoding, "\n") if $DEBUG;
79             }
80             1;
81              
82              
83             __END__