blob: 86f2877b476639abc4d54a2b05b761ebb66ef9f4 (
plain)
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
|
#!/usr/bin/env perl
# Challenge 039
#
# TASK #2
# Contributed by Andrezgz
# Write a script to demonstrate Reverse Polish notation(RPN). Checkout the wiki
# page for more information about RPN.
use Modern::Perl;
# simple rpn calculator
my @stack;
my %dispatch = (
'+' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a+$b; },
'-' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a-$b; },
'*' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a*$b; },
'/' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a/$b; },
'.' => sub { say pop @stack; },
);
for (split //, "@ARGV") {
if (/\s/) {}
elsif (/\d/) { push @stack, $_; }
elsif (exists $dispatch{$_}) { $dispatch{$_}->(); }
else { die "invalid operation: $_"; }
}
|