File Coverage

blib/lib/Convert/AnyBase.pm
Criterion Covered Total %
statement 8 16 50.0
branch n/a
condition 0 9 0.0
subroutine 3 8 37.5
pod 4 4 100.0
total 15 37 40.5


line stmt bran cond sub pod time code
1             package Convert::AnyBase;
2              
3 2     2   112165 use warnings;
  2         6  
  2         65  
4 2     2   11 use strict;
  2         2  
  2         784  
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             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             sub new {
82 1     1 1 86 shift;
83 1         734 require Convert::AnyBase::Converter;
84 0           return Convert::AnyBase::Converter->new( @_ );
85             }
86              
87             {
88             my ( $hex, $crockford, $decimal );
89            
90             sub hex {
91 0   0 0 1   return $hex || __PACKAGE__->new( set => ( join '', 0 .. 9, 'a' .. 'f' ), normalize => sub { lc } );
  0     0      
92             }
93            
94             sub crockford {
95             return $crockford ||= __PACKAGE__->new( set => ( join '', 0 .. 9, 'a' .. 'h', 'j', 'k', 'm', 'n', 'p' .. 't', 'v', 'w', 'x', 'y', 'z' ),
96 0     0     normalize => sub { s/[oO]/0/g; s/[iIlL]/1/g; lc },
  0            
  0            
97 0   0 0 1   );
98             }
99              
100             sub decimal {
101 0   0 0 1   return $decimal ||= __PACKAGE__->new( set => ( join '', 0 .. 9, ) );
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             1; # End of Convert::AnyBase