]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Units.pl
might as well strict
[infobot.git] / src / Modules / Units.pl
1 #   Units.pl: convert units of measurement
2 #     Author: M-J. Dominus (mjd-perl-units-id-iut+buobvys+@plover.com)
3 #    License: GPL, Copyright (C) 1996,1999
4 #       NOTE: Integrated into infobot by xk.
5
6 package Units;
7
8 use strict;
9
10 sub convertUnits {
11   my ($from,$to) = @_;
12
13   if ($from =~ /([+-]?[\d\.]+(?:e[+-]?[\d]+)?)\s+(temp[CFK])/){
14     $from = qq|${2}(${1})|;
15   }
16
17   my $units = new IO::File;
18   open $units, '-|', 'units', $from, $to or &::DEBUG("Unable to run units: $!") and return;
19   my $response = readline ($units);
20   if ($response =~ /\s+\*\s+([+-]?[\d\.]+(?:e[+-]?[\d]+)?)/ or $response =~ /\t([+-]?[\d\.]+(?:e[+-]?[\d]+)?)/){
21     &::performStrictReply(sprintf("$from is approximately \002%.6g\002 $to", $1));
22   }
23   else {
24     &::performStrictReply("$from cannot be converted to ${to}: $response");
25   }
26   return;
27 }
28
29 1;
30
31 # vim:ts=4:sw=4:expandtab:tw=80