Maths.pm

filter: Statementstmt 13.4% 11 / 82 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 3 / 5 Podpod 100% 2 / 2 totaltotal 12.7% 16 / 126 CC 19 SCAR 57.5
CC
19
Cov
9%
told
380.0
help
2.0
BEGIN
1.0
CRAP 314.1
↑ Summary LongURLs.pm »
1 package Bot::BasicBot::Pluggable::Module::Maths;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Bot::BasicBot::Pluggable::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our %digits = (
8 "first", "1",
9 "second", "2",
10 "third", "3",
11 "fourth", "4",
12 "fifth", "5",
13 "sixth", "6",
14 "seventh", "7",
15 "eighth", "8",
16 "ninth", "9",
17 "tenth", "10",
18 "one", "1",
19 "two", "2",
20 "three", "3",
21 "four", "4",
22 "five", "5",
23 "six", "6",
24 "seven", "7",
25 "eight", "8",
26 "nine", "9",
27 "ten", "10"
28 );
29  
30  
31  
32 0 ▶sub told {
Subroutine told: not called CC=19 SCAR=59.4
Pod documented
33 0 my ($self, $mess, $pri) = @_;
34  
35 0 my $in = $mess->{body};
36 0 my $orig = $in;
37 0 $in =~ s/\?$//; # trailing ?
38  
39 0 foreach my $x (keys %digits) {
40 0 $in =~ s/\b$x\b/$digits{$x}/g;
41 }
42  
43 0 ▶ return if $in =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /^\s*$/ 0 0 0
44 0 ▶ return if $in =~ /(\d+\.){2,}/;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /(\d+\.){2,}/ 0 0 0
45  
46 0 my $loc = $in;
47 0 ▶ while ($loc =~ /(exp ([\w\d]+))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ /(exp ([\w\d]+))/) 0 0 0
48 0 my $exp = $1;
49 0 my $val = exp($2);
50 0 $loc =~ s/$exp/+$val/g;
51 }
52  
53 0 ▶ while ($loc =~ /(hex2dec\s*([0-9A-Fa-f]+))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ /(hex2dec\s*([0-9A-Fa-f]+))/) 0 0 0
54 0 my $exp = $1;
55 0 my $val = hex($2);
56 0 $loc =~ s/$exp/+$val/g;
57 }
58
59 0 ▶ if ($loc =~ /^\s*(dec2hex\s*(\d+))\s*\?*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ /^\s*(dec2hex\s*(\d+))\s*\?*/) 0 0 0
60 0 my $exp = $1;
61 0 my $val = sprintf("%x", "$2");
62 0 $loc =~ s/$exp/+$val/g;
63 }
64 0 my $e = exp(1);
65 0 $loc =~ s/\be\b/$e/;
66  
67 0 ▶ while ($loc =~ /(log\s*((\d+\.?\d*)|\d*\.?\d+))\s*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ /(log\s*((\d+\.?\d*)|\d*\.?\d+))\s*/) 0 0 0
68 0 my $exp = $1;
69 0 my $res = $2;
70 0 my $val;
71 0 ▶ if ($res == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
72 0 $val = "Infinity";
73 } else {
74 0 $val = log($res);
75 }
76 0 $loc =~ s/$exp/+$val/g;
77 }
78  
79 0 ▶ while ($loc =~ /(bin2dec ([01]+))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ /(bin2dec ([01]+))/) 0 0 0
80 0 my $exp = $1;
81 0 my $val = join ('', unpack ("B*", $2)) ;
82 0 $loc =~ s/$exp/+$val/g;
83 }
84  
85 0 ▶ while ($loc =~ /(dec2bin (\d+))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ /(dec2bin (\d+))/) 0 0 0
86 0 my $exp = $1;
87 0 my $val = join('', unpack('B*', pack('N', $2)));
88 0 $val =~ s/^0+//;
89 0 $loc =~ s/$exp/+$val/g;
90 }
91  
92 0 $loc =~ s/ to the / ** /g;
93 0 $loc =~ s/\btimes\b/\*/g;
94 0 $loc =~ s/\bdiv(ided by)? /\/ /g;
95 0 $loc =~ s/\bover /\/ /g;
96 0 $loc =~ s/\bsquared/\*\*2 /g;
97 0 $loc =~ s/\bcubed/\*\*3 /g;
98 0 $loc =~ s/\bto\s+(\d+)(r?st|nd|rd|th)?( power)?/\*\*$1 /ig;
99 0 $loc =~ s/\bpercent of/*0.01*/ig;
100 0 $loc =~ s/\bpercent/*0.01/ig;
101 0 $loc =~ s/\% of\b/*0.01*/g;
102 0 $loc =~ s/\%/*0.01/g;
103 0 $loc =~ s/\bsquare root of (\d+)/$1 ** 0.5 /ig;
104 0 $loc =~ s/\bcubed? root of (\d+)/$1 **(1.0\/3.0) /ig;
105 0 $loc =~ s/ of / * /;
106 0 $loc =~ s/(bit(-| )?)?xor(\'?e?d( with))?/\^/g;
107 0 $loc =~ s/(bit(-| )?)?or(\'?e?d( with))?/\|/g;
108 0 $loc =~ s/bit(-| )?and(\'?e?d( with))?/\& /g;
109 0 $loc =~ s/(plus|and)/+/ig;
110  
111 0 ▶ return if $loc !~ /^\s*[-\d*+\s()\/^\.\|\&\*\!]+\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $loc =~ m[^\s*[-\d*+\s()/^\.\|\&\*\!]+\s*$] 0 0 0
112 0 ▶ return if $loc =~ /^\s*\(?\d+\.?\d*\)?\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $loc =~ /^\s*\(?\d+\.?\d*\)?\s*$/ 0 0 0
113 0 ▶ return if $loc =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $loc =~ /^\s*$/ 0 0 0
114 0 ▶ return if $loc =~ /^\s*[( )]+\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $loc =~ /^\s*[( )]+\s*$/ 0 0 0
115
116 0 $loc = eval($loc);
117 # I just realised that I could have done this with Maths::Expression and these checks
118 0 ▶ return unless $loc =~ /^[-+\de\.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $loc =~ /^[-+\de\.]+$/ 0 0 0
119  
120 0 $loc =~ s/\.0+$//;
121 0 $loc =~ s/(\.\d+)000\d+/$1/;
122 0 ▶ $loc = "a number with quite a few digits..." if (length($loc) > 30);
Branch: 1 branch
BranchTrueFalseTotal
if length $loc > 30 0 0 0
123 0 $orig =~ s!(^\s*|\s*$)!!g;
124 0 ▶ return if ($orig eq $loc) || ($orig == $loc);
Branch: 1 branch
BranchTrueFalseTotal
if $orig eq $loc or $orig == $loc 0 0 0
Condition0%0/3
$orig eq $loc or $orig == $loc
l!l&&r!l&&!r
000
MC/DC0%0/2
$orig eq $loc or $orig == $loc
$orig eq $loc $orig == $loc
Truth table0%0/3
A or B
A= $orig eq $locB= $orig == $loc
ABresult
1X1
011
000
125 0 return $loc;
126 }
127  
128 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
129 0 return "Commands: a maths expression";
130 }
131  
132 1 1;
133  
134 =head1 NAME
135  
136 Bot::BasicBot::Pluggable::Module::Maths - evaluate arbitary maths expressions
137  
138 =head1 SYNOPSIS
139  
140 Does everything that C<Math::Expression> does;
141  
142 =head1 AUTHOR
143  
144 Simon Wistow, <simon@thegestalt.org>
145  
146 based on code by Kevin Lenzo
147  
148 =head1 COPYRIGHT
149  
150 Copyright 2005, Simon Wistow
151  
152 Distributed under the same terms as Perl itself.
153  
154 =head1 SEE ALSO
155  
156 L<Math::Expression>
157  
158 =cut
159  
↑ Summary LongURLs.pm »