| 1 | package Acme::Bleach::Numerically; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.4 $ =~ /(\d+)/g; | |||||||||
| 7 | 2 | our $MAX_SIZE = 0x7fff_ffff; | |||||||||
| 8 | 2 | ▶ | use Math::BigInt lib => 'GMP'; # faster if there, fallbacks if not | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Math::BigFloat; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Math::BigRat; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 26 | ▶ | sub str2num{ | ||||||||
|
Subroutine
str2num: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 13 | 26 | my $str = shift; | |||||||||
| 14 | 26 | ▶ | return 0 if $str eq ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 22 | Math::BigFloat->accuracy(length($str) * 8); | |||||||||
| 16 | 22 | my $bnum = Math::BigFloat->new(0); | |||||||||
| 17 | 22 | my $bden = Math::BigInt->new(256); | |||||||||
| 18 | 22 | $bden **= length($str); | |||||||||
| 19 | 22 | for my $ord (unpack "C*", $str){ | |||||||||
| 20 | 1485 | $bnum = $bnum * 256 + $ord; | |||||||||
| 21 | } | ||||||||||
| 22 | 22 | $bnum /= $bden; | |||||||||
| 23 | 22 | $bnum =~ s/0+$//o; | |||||||||
| 24 | 22 | return $bnum; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 26 | ▶ | sub num2str{ | ||||||||
|
Subroutine
num2str: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 28 | 26 | my $num = shift; | |||||||||
| 29 | 26 | ▶ | return '' unless $num; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 22 | my $bnum = Math::BigFloat->new($num); | |||||||||
| 31 | 22 | my $str = ''; | |||||||||
| 32 | 22 | ▶ | while($bnum > 0){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 1485 | $bnum *= 256; | |||||||||
| 34 | 1485 | my $ord = int $bnum->copy; | |||||||||
| 35 | 1485 | $str .= chr $ord; | |||||||||
| 36 | 1485 | $bnum -= $ord; | |||||||||
| 37 | } | ||||||||||
| 38 | 22 | return $str; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 2 | ▶ | sub import{ | ||||||||
|
Subroutine
import: called CC=8 SCAR=29.9
| |||||||||||
| 42 | 2 | my $class = shift; | |||||||||
| 43 | 2 | ▶ | if (@_){ # behave nicely | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 2 | my ($pkg, $filename, $line) = caller; | |||||||||
| 45 | 2 | for my $arg (@_){ | |||||||||
| 46 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 5 | ▶ | next unless defined &{ "$arg" }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 4 | *{ $pkg . "::$arg" } = \&{ "$arg" }; | |||||||||
| 49 | } | ||||||||||
| 50 | }else{ # bleach! | ||||||||||
| 51 | 0 | ▶ | open my $in, "<:raw", $0 or die "$0 : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | my $src = join '', grep !/use\s*Acme::Bleach::Numerically/, <$in>; | |||||||||
| 53 | 0 | close $in; | |||||||||
| 54 | # warn $src; | ||||||||||
| 55 | 0 | ▶ | if ($src =~ /^0\.[0-9]+;?\s*$/){ # bleached | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | my $code = num2str($src); | |||||||||
| 57 | 0 | eval $code; | |||||||||
| 58 | }else{ # whiten | ||||||||||
| 59 | { | ||||||||||
| 60 | 0 | ▶ | no warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||
| 61 | 0 | eval $src; | |||||||||
| 62 | 0 | ▶ | if ($@){ # dirty | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $@ =~ s/\(eval \d+\)/$0/eg; | |||||||||
| 64 | 0 | die $@; | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | 0 | ▶ | open my $out, ">:raw", $0 or die "$0 : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | print $out | |||||||||
| 69 | "use ", __PACKAGE__, ";\n", | ||||||||||
| 70 | str2num($src), "\n"; | ||||||||||
| 71 | } | ||||||||||
| 72 | 0 | exit; | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | 2 | 1; | |||||||||
| 76 | __END__ | ||||||||||