| 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 <abw@wardley.org> | ||||||||||
| 10 | # | ||||||||||
| 11 | #======================================================================== | ||||||||||
| 12 | |||||||||||
| 13 | package Badger::Codec::Encoding; | ||||||||||
| 14 | |||||||||||
| 15 | use Badger::Class | ||||||||||
| 16 | 3 | ▶ | version => 0.01, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | debug => 0, | ||||||||||
| 18 | base => 'Badger::Codec', | ||||||||||
| 19 | import => 'class', | ||||||||||
| 20 | constants => 'PKG', | ||||||||||
| 21 | constant => { | ||||||||||
| 22 | encoding => 'ASCII', | ||||||||||
| 23 | 3 | }; | |||||||||
| 24 | |||||||||||
| 25 | 3 | ▶ | use Encode qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 3 | ▶ | use bytes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 1 | ▶ | sub encode { | ||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | # replace $self with $self->encoding | ||||||||||
| 30 | 1 | 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 | goto &Encode::encode; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | sub decode { | ||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | # as above: monkey with the args so the goto works as expected | ||||||||||
| 38 | 1 | unshift(@_, shift->encoding); | |||||||||
| 39 | 1 | goto &Encode::decode; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 3 | ▶ | sub encoder { | ||||||||
|
Subroutine
encoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 3 | my $self = shift; | |||||||||
| 44 | 3 | my $enc = $self->encoding; | |||||||||
| 45 | 1 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 1 | unshift(@_, $enc); | |||||||||
| 47 | 1 | goto &Encode::encode | |||||||||
| 48 | 3 | }; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 3 | ▶ | sub decoder { | ||||||||
|
Subroutine
decoder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 3 | my $self = shift; | |||||||||
| 53 | 3 | my $enc = $self->encoding; | |||||||||
| 54 | 1 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 1 | unshift(@_, $enc); | |||||||||
| 56 | 1 | goto &Encode::decode | |||||||||
| 57 | 3 | }; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | #----------------------------------------------------------------------- | ||||||||||
| 62 | # generate subclasses for various UTF encodings | ||||||||||
| 63 | #----------------------------------------------------------------------- | ||||||||||
| 64 | |||||||||||
| 65 | 3 | my $base = __PACKAGE__; | |||||||||
| 66 | 3 | my @encs = qw( utf8 UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE ); | |||||||||
| 67 | |||||||||||
| 68 | 3 | for my $enc (@encs) { | |||||||||
| 69 | 18 | my $name = $enc; | |||||||||
| 70 | 18 | $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 | 18 | my $subclass = class($base.PKG.$name); | |||||||||
| 75 | 18 | $subclass->base($base); | |||||||||
| 76 | 18 | $subclass->constant( encoding => $enc ); | |||||||||
| 77 | |||||||||||
| 78 | 18 | ▶ | $base->debug("$name => $enc => ", $subclass->name->encoding, "\n") if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 3 | 1; | |||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | __END__ | ||||||||||