From 7139490067fac0205b91f5afe125ed93120c9bab Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Fri, 8 May 2015 05:02:35 -0700 Subject: [PATCH] Updated math.pl to perform unit conversions --- doc/MODULES | 1 + modules/math.pl | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/MODULES b/doc/MODULES index 4eff4c81..5000892b 100644 --- a/doc/MODULES +++ b/doc/MODULES @@ -13,6 +13,7 @@ LWP::Protocol::https LWP::Simple LWP::UserAgent LWP::UserAgent::WithCache +Math::Units Net::Dict Proc::ProcessTable SOAP::Lite diff --git a/modules/math.pl b/modules/math.pl index 1503d3e6..89e986ec 100755 --- a/modules/math.pl +++ b/modules/math.pl @@ -2,7 +2,9 @@ # Quick and dirty by :pragma -my ($arguments, $response, $invalid); +use Math::Units qw(convert); + +my ($arguments, $response, $invalid, @conversion); my @valid_keywords = ( 'sin', 'cos', 'tan', 'atan', 'exp', 'int', 'hex', 'oct', 'log', 'sqrt', @@ -22,6 +24,10 @@ my $orig_arguments = $arguments; $arguments =~ s/(the )*answer.*question of life(,? the universe and everything)?\s?/42/gi; $arguments =~ s/meaning of (life|existence|everything)?/42/gi; +if ($arguments =~ s/([^ ]+)\s+to\s+([^ ]+)\s*$//) { + @conversion = ($1, $2); +} + if($arguments =~ m/([\$`\|{}"'#@=?\[\]])/ or $arguments =~ m/(~~)/) { $invalid = $1; } else { @@ -49,4 +55,13 @@ if($@) { exit 1; } +if (@conversion) { + my $result = eval { convert($response, $conversion[0], $conversion[1]); }; + if ($@) { + print "Unknown conversion from $conversion[0] to $conversion[1]. Units are case-sensitive (Hz, not hz).\n"; + exit 1; + } + $response = "$result $conversion[1]"; +} + print "$orig_arguments = $response\n";