| 1 | # | ||||||||||
| 2 | # Copyright (c) 2003-2019 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Chj::BinHexOctDec | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | use Chj::BinHexOctDec; | ||||||||||
| 17 | my $num = Chj::BinHexOctDec->bin("1001010010"); | ||||||||||
| 18 | is ref($num), "Chj::BinHexOctDec"; | ||||||||||
| 19 | is $num->dec, 594; | ||||||||||
| 20 | |||||||||||
| 21 | use FP::Equal 'is_equal'; | ||||||||||
| 22 | is_equal [ map { $num->$_ } qw(dec bin hex oct) ], | ||||||||||
| 23 | [ 594, "1001010010", "252", "1122" ]; | ||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||
| 28 | |||||||||||
| 29 | Conversions between number bases. | ||||||||||
| 30 | |||||||||||
| 31 | The methods are overloaded both as class methods (to convert *from* | ||||||||||
| 32 | that base) and object method (*to* that base). | ||||||||||
| 33 | |||||||||||
| 34 | Note: does not die on invalid input. (Should this be considered a | ||||||||||
| 35 | bug?) | ||||||||||
| 36 | |||||||||||
| 37 | =head1 METHODS | ||||||||||
| 38 | |||||||||||
| 39 | =head1 NOTE | ||||||||||
| 40 | |||||||||||
| 41 | This is alpha software! Read the status section in the package README | ||||||||||
| 42 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | package Chj::BinHexOctDec; | ||||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub bin { | ||||||||
|
Subroutine
bin: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 53 | 0 | my $this = shift; | |||||||||
| 54 | 0 | ▶ | if (ref $this) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | sprintf('%b', $$this) | |||||||||
| 56 | } else { | ||||||||||
| 57 | 0 | my $data = oct('0b' . shift); | |||||||||
| 58 | 0 | bless \$data, $this | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub dec { | ||||||||
|
Subroutine
dec: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 63 | 0 | my $this = shift; | |||||||||
| 64 | 0 | ▶ | if (ref $this) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | $$this | |||||||||
| 66 | } else { | ||||||||||
| 67 | 0 | my $data = shift; | |||||||||
| 68 | 0 | bless \$data, $this | |||||||||
| 69 | } | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub oct { | ||||||||
|
Subroutine
oct: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 73 | 0 | my $this = shift; | |||||||||
| 74 | 0 | ▶ | if (ref $this) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | sprintf('%o', $$this) | |||||||||
| 76 | } else { | ||||||||||
| 77 | 0 | my $data = oct('0' . shift); | |||||||||
| 78 | 0 | bless \$data, $this | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub hex { | ||||||||
|
Subroutine
hex: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 83 | 0 | my $this = shift; | |||||||||
| 84 | 0 | ▶ | if (ref $this) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | sprintf('%x', $$this) | |||||||||
| 86 | } else { | ||||||||||
| 87 | 0 | my $data = hex(shift); # oct('0x'.shift); should work as well | |||||||||
| 88 | 0 | bless \$data, $this | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 1 | 1 | |||||||||