]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Units.pl
79e50b3aed777b6a79475ab03a9a0376af173151
[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
19       or &::DEBUG("Unable to run units: $!")
20       and return;
21     my $response = readline($units);
22     if (   $response =~ /\s+\*\s+([+-]?[\d\.]+(?:e[+-]?[\d]+)?)/
23         or $response =~ /\t([+-]?[\d\.]+(?:e[+-]?[\d]+)?)/ )
24     {
25         &::performStrictReply(
26             sprintf( "$from is approximately \002%.6g\002 $to", $1 ) );
27     }
28     else {
29         &::performStrictReply("$from cannot be converted to ${to}: $response");
30     }
31     return;
32 }
33
34 1;
35
36 # vim:ts=4:sw=4:expandtab:tw=80