File Coverage

blib/lib/Bot/Cobalt/Plugin/Ohm.pm
Criterion Covered Total %
statement 31 55 56.3
branch 11 36 30.5
condition 5 36 13.8
subroutine 8 12 66.6
pod 0 4 0.0
total 55 143 38.4


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Plugin::Ohm;
2             $Bot::Cobalt::Plugin::Ohm::VERSION = '1.001001';
3             # A simple Ohm's law calculator borrowed from SYMKAT:
4             # https://gist.github.com/symkat/da287f0993e708b53701
5              
6 2     2   39062 use strictures 2;
  2         3411  
  2         87  
7              
8 2     2   1666 use Bot::Cobalt;
  2         20211  
  2         12  
9 2     2   3283 use Bot::Cobalt::Common;
  2         502489  
  2         17  
10              
11 2     2   17634 use Regexp::Common 'number';
  2         4980  
  2         8  
12              
13 2     2 0 140 sub new { bless [], shift }
14              
15             sub Cobalt_register {
16 0     0 0 0 my ($self, $core) = splice @_, 0, 2;
17              
18 0         0 my @events = map {; 'public_cmd_'.$_ } qw/ ohm watt amp volt /;
  0         0  
19 0         0 register $self, SERVER => [ @events ];
20              
21 0         0 $core->log->info("Loaded Ohm");
22              
23 0         0 PLUGIN_EAT_NONE
24             }
25              
26             sub Cobalt_unregister {
27 0     0 0 0 my ($self, $core) = splice @_, 0, 2;
28 0         0 $core->log->info("Unloaded Ohm");
29 0         0 PLUGIN_EAT_NONE
30             }
31              
32             {
33 2     2   6606 no strict 'refs';
  2         4  
  2         1572  
34             for (qw/watt amp volt/) {
35             my $meth = 'Bot_public_cmd_'.$_;
36             *{__PACKAGE__.'::'.$meth} = *Bot_public_cmd_ohm
37             }
38             }
39              
40             sub Bot_public_cmd_ohm {
41 0     0 0 0 my ($self, $core) = splice @_, 0, 2;
42 0         0 my $msg = ${ $_[0] };
  0         0  
43              
44 0         0 my $context = $msg->context;
45 0         0 my $src_nick = $msg->src_nick;
46            
47 0         0 my $str = join '', @{ $msg->message_array };
  0         0  
48 0         0 my %parsed = $self->_parse_values($str);
49              
50             my $resp = keys %parsed ?
51 0     0   0 try { $self->_calc(%parsed) } catch { "Calc failure; $_" }
  0         0  
52 0 0       0 : "Parser failure; try input in the form of: a

w o v";

53              
54 0         0 broadcast message => $context, $msg->channel, "${src_nick}: $resp";
55            
56 0         0 PLUGIN_EAT_NONE
57             }
58              
59             # These routines stolen directly from SYMKAT and then hacked to shreds:
60              
61             sub _parse_values {
62 6     6   2206 my ($self, $message) = @_;
63 6         8 my %values;
64              
65 6         10 for my $unit (qw/o w a v/) {
66 24 100       1682 if ($message =~ m/($RE{num}{real})$unit/i) {
67 12         2100 $values{$unit} = $1
68             }
69             }
70              
71             %values
72 6         331 }
73              
74             sub _calc {
75 3     3   370 my ($self, %values) = @_;
76             # A = V / O
77             # A = W / V
78             # A = sqrt(W / O)
79 3 50       8 unless (defined $values{a}) {
80             $values{a} =
81             $values{v} && $values{o} ? $values{v} / $values{o}
82             : $values{w} && $values{v} ? $values{w} / $values{v}
83             : $values{w} && $values{o} ? sqrt( $values{w} / $values{o} )
84 3 50 66     32 : die "Not enough information to calculate amperage\n"
    50 33        
    100 33        
85             ;
86             }
87             # W = ( V * V ) / O
88             # W = ( A * A ) * O
89             # W = V * R
90 2 50       8 unless (defined $values{w}) {
91             $values{w} =
92             $values{v} && $values{o} ? ($values{v} ** 2) / $values{o}
93             : $values{a} && $values{o} ? ($values{a} ** 2) * $values{o}
94             : $values{v} && $values{a} ? $values{v} * $values{a}
95 2 0 33     21 : die "Not enough information to calculate wattage\n"
    0 0        
    50 0        
96             ;
97             }
98             # O = V / A
99             # O = ( V * V ) * W
100             # O = W / ( A * A )
101 2 50       7 unless (defined $values{o}) {
102             $values{o} =
103             $values{v} && $values{a} ? $values{v} / $values{a}
104             : $values{v} && $values{w} ? ($values{v} ** 2) * $values{w}
105 0 0 0     0 : $values{w} && $values{a} ? $values{w} / ($values{a} ** 2)
    0 0        
    0 0        
106             : die "Not enough information to calculate ohms\n"
107             ;
108             }
109             # V = sqrt( W * O )
110             # V = W / A
111             # V = A * O
112 2 50       6 unless (defined $values{v}) {
113             $values{v} =
114             $values{w} && $values{o} ? sqrt( $values{w} * $values{o} )
115             : $values{w} && $values{a} ? $values{w} / $values{a}
116             : $values{a} && $values{o} ? $values{a} * $values{o}
117 0 0 0     0 : die "Not enough information to calculate voltage\n"
    0 0        
    0 0        
118             }
119              
120             sprintf
121             "%.2fw/%.2fv @ %.2famps against %.2fohm" =>
122 2         4 map {; $values{$_} } qw/ w v a o /
  8         39  
123             }
124              
125             1;
126              
127             =pod
128              
129             =head1 NAME
130              
131             Bot::Cobalt::Plugin::Ohm - Simple Ohm's law calculator for Bot::Cobalt
132              
133             =head1 SYNOPSIS
134              
135             # What's my voltage and amperage firing my 0.87 Ohm coil at 25W?
136             !ohm 0.87o 25w
137              
138             =head1 DESCRIPTION
139              
140             A simple Ohm's law calculator; given a string specifying parameters in the
141             form of C<< a o w v >>, attempts to fill in the blanks.
142              
143             =head1 AUTHOR
144              
145             Kaitlyn Parkhurst (CPAN: C) wrote the calculator as an irssi script.
146              
147             Adapted (with permission) to L by Jon Portnoy
148              
149             =cut