-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.pl
executable file
·46 lines (40 loc) · 951 Bytes
/
math.pl
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
#!/usr/bin/perl
use Math::Expression::Evaluator;
use Try::Tiny;
sub trim {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub math {
my $plain = shift;
my $evaluator = Math::Expression::Evaluator->new;
try {
my $result = $evaluator->parse($plain)->val();
return $result;
}catch {
return "Fail";
}
}
sub main {
my $plain = shift;
$plain = trim($plain);
my $result = math($plain);
return "[${plain}] ${result}";
}
sub event_privmsg {
my ($server, $data, $nick, $address) = @_;
my ($target, $text) = split(/ :/, $data, 2);
$target = $nick if($target !~ /^#/);
return unless($text =~ /계산\?(.+)?/);
$server->command("MSG ${target} ".main($+));
}
if(caller) {
require Irssi;
Irssi::signal_add("event privmsg", "event_privmsg");
}else {
binmode(STDOUT, ":utf8");
print main(@ARGV[0]);
print "\n";
}