line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
1891
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
80
|
|
2
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2066
|
|
3
|
|
|
|
|
|
|
package Math::Calculator; |
4
|
|
|
|
|
|
|
{ |
5
|
|
|
|
|
|
|
$Math::Calculator::VERSION = '1.022'; |
6
|
|
|
|
|
|
|
} |
7
|
|
|
|
|
|
|
# ABSTRACT: a multi-stack calculator class |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
2
|
|
|
2
|
1
|
1781
|
bless { |
12
|
|
|
|
|
|
|
stacks => { default => [] }, |
13
|
|
|
|
|
|
|
current_stack => 'default' |
14
|
|
|
|
|
|
|
} => shift |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub current_stack { |
19
|
68
|
|
|
68
|
1
|
95
|
my ($self, $stack) = @_; |
20
|
|
|
|
|
|
|
|
21
|
68
|
100
|
100
|
|
|
471
|
return $self->{current_stack} unless $stack and $stack =~ /^\w+$/; |
22
|
2
|
100
|
|
|
|
13
|
$self->{stacks}{$stack} = [] unless defined $self->{stacks}{$stack}; |
23
|
2
|
|
|
|
|
12
|
$self->{current_stack} = $stack; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
76
|
100
|
|
76
|
1
|
315
|
sub stack { $_[0]->{stacks}->{$_[1] ? $_[1] : $_[0]->current_stack} } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
5
|
|
|
5
|
1
|
13
|
sub top { (shift)->stack->[-1] } |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
6
|
|
|
6
|
1
|
14
|
sub clear { @{(shift)->stack} = (); } |
|
6
|
|
|
|
|
20
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub push { ## no critic |
37
|
36
|
|
|
36
|
1
|
56
|
push @{(shift)->stack}, @_; |
|
36
|
|
|
|
|
67
|
|
38
|
|
|
|
|
|
|
} |
39
|
3
|
|
|
3
|
1
|
3
|
sub push_to { CORE::push @{(shift)->stack(shift)}, @_; } |
|
3
|
|
|
|
|
7
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub pop { ## no critic |
43
|
17
|
100
|
|
17
|
1
|
22
|
splice @{$_[0]->stack}, - (defined $_[1] ? $_[1] : 1); |
|
17
|
|
|
|
|
40
|
|
44
|
|
|
|
|
|
|
} |
45
|
3
|
100
|
|
3
|
1
|
4
|
sub pop_from { splice @{$_[0]->stack($_[1])}, - (defined $_[2] ? $_[2] : 1); } |
|
3
|
|
|
|
|
8
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
2
|
|
|
2
|
1
|
10
|
sub from_to { $_[0]->push_to($_[2], $_[0]->pop_from($_[1], $_[3])) } |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
2
|
1
|
8
|
sub dupe { $_[0]->push( $_[0]->top ); } |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# sub _op_two { $_[0]->push( $_[1]->( $_[0]->pop(2) ) ); $_[0]->top; } |
55
|
|
|
|
|
|
|
|
56
|
14
|
|
|
14
|
|
44
|
sub _op_two { ($_[0]->_op_n(2, $_[1]))[-1] } |
57
|
|
|
|
|
|
|
sub _op_n { |
58
|
16
|
|
|
16
|
|
704
|
$_[0]->push(my @r = $_[2]->( $_[0]->pop($_[1]) )); |
59
|
16
|
100
|
|
|
|
105
|
wantarray ? @r : $r[-1] |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
2
|
1
|
6
|
sub twiddle { (shift)->_op_two( sub { $_[1], $_[0] } ); } |
|
2
|
|
|
2
|
|
11
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
4
|
|
|
4
|
1
|
567
|
sub add { (shift)->_op_two( sub { (shift) + (shift) } ); } |
|
4
|
|
|
4
|
|
30
|
|
67
|
1
|
|
|
1
|
1
|
4
|
sub subtract { (shift)->_op_two( sub { (shift) - (shift) } ); } |
|
1
|
|
|
1
|
|
8
|
|
68
|
1
|
|
|
1
|
1
|
5
|
sub multiply { (shift)->_op_two( sub { (shift) * (shift) } ); } |
|
1
|
|
|
1
|
|
7
|
|
69
|
2
|
|
|
2
|
1
|
9
|
sub divide { (shift)->_op_two( sub { (shift) / (shift) } ); } |
|
2
|
|
|
2
|
|
15
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
## no critic Subroutines::ProhibitBuiltinHomonyms |
73
|
1
|
|
|
1
|
1
|
6
|
sub modulo { (shift)->_op_two( sub { (shift) % (shift) } ); } |
|
1
|
|
|
1
|
|
8
|
|
74
|
1
|
|
|
1
|
1
|
3
|
sub sqrt { my ($self) = @_; $self->push(2); $self->root; } |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
75
|
|
|
|
|
|
|
## use critic |
76
|
1
|
|
|
1
|
1
|
5
|
sub raise_to { (shift)->_op_two( sub { (shift) **(shift) } ); } |
|
1
|
|
|
1
|
|
7
|
|
77
|
2
|
|
|
2
|
1
|
40
|
sub root { (shift)->_op_two( sub { (shift)**(1/(shift)) } ); } |
|
2
|
|
|
2
|
|
11
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
2
|
|
|
2
|
|
3
|
sub _quorem { my ($n,$m) = @_; (int($n/$m), $n % $m) } |
|
2
|
|
|
|
|
14
|
|
81
|
1
|
|
|
1
|
1
|
5
|
sub quorem { (shift)->_op_n(2, \&_quorem ); } |
82
|
1
|
|
|
1
|
1
|
6
|
sub divmod { (shift)->_op_n(2, \&_quorem ); } |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |