AnyBase.pm

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 78.7% 37 / 47 CC 4 SCAR 4.8
CC
4
Cov
86%
hex
2.3
decimal
2.1
crockford
2.0
CRAP 1.6
« AnyBase/Converter.pm ↑ Summary AnyBase/Carp.pm »
1 package Convert::AnyBase;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Convert::AnyBase - Convert (encode/decode) numbers to and from an arbitrary base
9  
10 =head1 VERSION
11  
12 Version 0.010
13  
14 =cut
15  
16 2 our $VERSION = '0.010';
17  
18 =head1 SYNOPSIS
19  
20 use Convert::AnyBase
21  
22 # A hex encoder/decoder
23 my $hex = Convert::AnyBase->new( set => '0123456789abcdef', normalize => sub { lc } )
24 $hex->encode( 10 ) # a
25 $hex->encode( 100 ) # 64
26 $hex->decode( 4d2 ) # 1234
27  
28 # A Crockford encoder/decoder (http://www.crockford.com/wrmg/base32.html)
29 Convert::AnyBase->new( set => ( join '', 0 .. 9, 'a' .. 'h', 'j', 'k', 'm', 'n', 'p' .. 't', 'v', 'w', 'x', 'y', 'z' ),
30 normalize => sub { s/[oO]/0/g; s/[iIlL]/1/g; lc }, # o, O => 0 / i, I, l, L => 1
31 )
32  
33 =head1 DESCRIPTION
34  
35 Convert::AnyBase is a tool for converting numbers to and from arbitrary symbol sets.
36  
37 =head1 USAGE
38  
39 =head2 $converter = Convert::AnyBase->new( ... )
40  
41 Create a new converter for the given base. The arguments are:
42  
43 set A string representing the base 'alphabet'. Each character is a different symbol for the base.
44 The length of the string is the base of the system. The 0-value is the first character, the
45 1-value is the second character, etc. For example, hexadecimal would be represented by the following:
46
47 '0123456789abcdef'
48  
49 normalize A code reference for normalizing a string before decoding. The code should operate on $_
50 and return the sanitized string. The normalizer can be used to consistently lowercase,
51 uppercase, or canocalize input, etc. A normalizer for Crockford (base 32):
52
53 sub {
54 s/[oO]/0/g; # Translate o/O to 0
55 s/[iIlL]/1/g; # Translate i/I/l/L to 1
56 lc; # Lowercase and return the result
57 }
58  
59 =head2 $string = $converter->encode( <number> )
60  
61 Encode <number> into a string
62  
63 =head2 $number = $converter->decode( <string> )
64  
65 Decode <string> into a number
66  
67 =head2 Convert::AnyBase->hex
68  
69 A hex converter
70  
71 =head2 Convert::AnyBase->decimal
72  
73 A decimal (string) converter
74  
75 =head2 Convert::AnyBase->crockford
76  
77 A Crockford converter
78  
79 =cut
80  
81 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
82 20 shift;
83 20 require Convert::AnyBase::Converter;
84 20 return Convert::AnyBase::Converter->new( @_ );
85 }
86  
87 {
88 2 my ( $hex, $crockford, $decimal );
89
90 16 ▶ sub hex {
Subroutine hex: called CC=2 SCAR=8.1
Pod documented
91 16 ▶ return $hex || __PACKAGE__->new( set => ( join '', 0 .. 9, 'a' .. 'f' ), normalize => sub { lc } );
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$hex || __PACKAGE__->new('set', join('', 0..9, ('a', 'b', 'c', 'd', 'e', 'f')), 'normalize', sub { lc $_; } )
l!l&&r!l&&!r
0160
MC/DC0%0/2
$hex || __PACKAGE__->new('set', join('', 0..9, ('a', 'b', 'c', 'd', 'e', 'f')), 'normalize', sub { lc $_; } )
$hex __PACKAGE__->new('set', join('', 0..9, ('a', 'b', 'c', 'd', 'e', 'f')), 'normalize', sub { lc $_; } )
Truth table33%1/3
A || B
A= $hexB= __PACKAGE__->new('set', join('', 0..9, ('a', 'b', 'c', 'd', 'e', 'f')), 'normalize', sub { lc $_; } )
ABresult
1X1
011
000
92 }
93
94 16 ▶ sub crockford {
Subroutine crockford: called CC=2 SCAR=7.0
Pod documented
95 return $crockford ||= __PACKAGE__->new( set => ( join '', 0 .. 9, 'a' .. 'h', 'j', 'k', 'm', 'n', 'p' .. 't', 'v', 'w', 'x', 'y', 'z' ),
96 8 ▶ normalize => sub { s/[oO]/0/g; s/[iIlL]/1/g; lc },
Subroutine __ANON__: called CC=1 SCAR=0.0
97 16 ▶ );
Condition66%2/3
$crockford ||= __PACKAGE__->new("set", join("", 0..9, ("a", "b", "c", "d", "e", "f", "g", "h"), "j", "k", "m", "n", ("p", "q", "r", "s", "t"), "v", "w", "x", "y", "z"), "normalize", sub { s/[oO]/0/g; s/[iIlL]/1/g; lc $_; } )
l!l&&r!l&&!r
1510
MC/DC0%0/2
$crockford ||= __PACKAGE__->new("set", join("", 0..9, ("a", "b", "c", "d", "e", "f", "g", "h"), "j", "k", "m", "n", ("p", "q", "r", "s", "t"), "v", "w", "x", "y", "z"), "normalize", sub { s/[oO]/0/g; s/[iIlL]/1/g; lc $_; } )
$crockford __PACKAGE__->new("set", join("", 0..9, ("a", "b", "c", "d", "e", "f", "g", "h"), "j", "k", "m", "n", ("p", "q", "r", "s", "t"), "v", "w", "x", "y", "z"), "normalize", sub { s/[oO]/0/g; s/[iIlL]/1/g; lc $_; } )
Truth table66%2/3
A ||= B
A= $crockfordB= __PACKAGE__->new("set", join("", 0..9, ("a", "b", "c", "d", "e", "f", "g", "h"), "j", "k", "m", "n", ("p", "q", "r", "s", "t"), "v", "w", "x", "y", "z"), "normalize", sub { s/[oO]/0/g; s/[iIlL]/1/g; lc $_; } )
ABresult
1X1
011
000
98 }
99  
100 2 ▶ sub decimal {
Subroutine decimal: called CC=2 SCAR=7.2
Pod documented
101 2 ▶ return $decimal ||= __PACKAGE__->new( set => ( join '', 0 .. 9, ) );
Condition66%2/3
$decimal ||= __PACKAGE__->new("set", join("", 0..9))
l!l&&r!l&&!r
110
MC/DC0%0/2
$decimal ||= __PACKAGE__->new("set", join("", 0..9))
$decimal __PACKAGE__->new("set", join("", 0..9))
Truth table66%2/3
A ||= B
A= $decimalB= __PACKAGE__->new("set", join("", 0..9))
ABresult
1X1
011
000
102 }
103 }
104  
105 =head1 SEE ALSO
106  
107 L<Encode::Base32::Crockford>
108  
109 L<Convert::BaseN>
110  
111 L<Math::BaseCnv>
112  
113 L<Math::NumberBase>
114  
115 =head1 AUTHOR
116  
117 Robert Krimen, C<< <rkrimen at cpan.org> >>
118  
119 =head1 BUGS
120  
121 Please report any bugs or feature requests to C<bug-convert-anybase at rt.cpan.org>, or through
122 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Convert-AnyBase>. I will be notified, and then you'll
123 automatically be notified of progress on your bug as I make changes.
124  
125  
126  
127  
128 =head1 SUPPORT
129  
130 You can find documentation for this module with the perldoc command.
131  
132 perldoc Convert::AnyBase
133  
134  
135 You can also look for information at:
136  
137 =over 4
138  
139 =item * RT: CPAN's request tracker
140  
141 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Convert-AnyBase>
142  
143 =item * AnnoCPAN: Annotated CPAN documentation
144  
145 L<http://annocpan.org/dist/Convert-AnyBase>
146  
147 =item * CPAN Ratings
148  
149 L<http://cpanratings.perl.org/d/Convert-AnyBase>
150  
151 =item * Search CPAN
152  
153 L<http://search.cpan.org/dist/Convert-AnyBase/>
154  
155 =back
156  
157  
158 =head1 ACKNOWLEDGEMENTS
159  
160  
161 =head1 COPYRIGHT & LICENSE
162  
163 Copyright 2009 Robert Krimen, all rights reserved.
164  
165 This program is free software; you can redistribute it and/or modify it
166 under the same terms as Perl itself.
167  
168  
169 =cut
170  
171 2 1; # End of Convert::AnyBase
« AnyBase/Converter.pm ↑ Summary AnyBase/Carp.pm »