1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package Plugin::Convert;
use strict;
use warnings;
use IPC::Open2;
sub configure {
my $self = shift;
my $cmdref = shift;
$cmdref->($self, "convert", sub { $self->convert(@_); } );
return $self;
}
sub convert {
my ($self, $irc, $logger, $who, $where, $ided, $rest, $no_reenter, @arguments) = @_;
my $from = (split / to /, $rest)[0];
my $to = (split / to /, $rest)[1];
return "Syntax: convert <from> [to <to>]\n" unless ($from);
my ($out, $in);
my $pid;
if ($to) {
$pid = open2($out, $in, 'units', '-1', '--compact', '--quiet', $from, $to);
} else {
$pid = open2($out, $in, 'units', '-1', '--compact', '--quiet', $from);
}
my $converted = <$out>;
chomp $converted;
close($in);
waitpid($pid, 0);
my $exit_status = $? >> 8;
# `units` doesn't actually seem to set this non-zero, but use it anyway
return "Error: $converted" if $exit_status;
if ($to) {
return "Convert $from -> $to: $converted\n";
} else {
return "Define $from: $converted\n";
}
}
1;
|