Updated math.pl to perform unit conversions

This commit is contained in:
Pragmatic Software 2015-05-08 05:02:35 -07:00
parent d7f2eb0d45
commit 7139490067
2 changed files with 17 additions and 1 deletions

View File

@ -13,6 +13,7 @@ LWP::Protocol::https
LWP::Simple
LWP::UserAgent
LWP::UserAgent::WithCache
Math::Units
Net::Dict
Proc::ProcessTable
SOAP::Lite

View File

@ -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";