| 1 | package # hide from PAUSE indexer | ||||||||||||||
| 2 | Calculator; | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Types::Standard qw( Num Str ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 3 | has 'left' => ( is => 'rw', isa => Num, default => 0 ); | |||||||||||||
| 10 | 3 | has 'right' => ( is => 'rw', isa => Str, default => '' ); | |||||||||||||
| 11 | 3 | has 'operator' => ( is => 'rw', isa => Str, default => '+' ); | |||||||||||||
| 12 | |||||||||||||||
| 13 | 3 | has 'display' => ( is => 'rw', isa => Str, default => '0' ); | |||||||||||||
| 14 | 3 | has 'equals' => ( is => 'rw', isa => Str, default => '' ); | |||||||||||||
| 15 | |||||||||||||||
| 16 | 128 | ▶ | sub key_in { | ||||||||||||
|
Subroutine
key_in: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 17 | 128 | my ( $self, $seq ) = @_; | |||||||||||||
| 18 | 128 | my @possible = grep { /\S/ } split( //, $seq ); | |||||||||||||
| 19 | 128 | $self->press($_) for @possible; | |||||||||||||
| 20 | } | ||||||||||||||
| 21 | |||||||||||||||
| 22 | 566 | ▶ | sub press { | ||||||||||||
|
Subroutine
press: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 23 | 566 | my ( $self, $key ) = @_; | |||||||||||||
| 24 | |||||||||||||||
| 25 | # Numbers | ||||||||||||||
| 26 | 566 | ▶ | $self->digit($1) if $key =~ m/^([\d\.])$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | # Operators | ||||||||||||||
| 29 | 566 | ▶ | $self->key_operator($1) if $key =~ m/^([\+\-\/\*])$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | # Equals | ||||||||||||||
| 32 | 566 | ▶ | $self->equalsign if $key eq '='; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | # Clear | ||||||||||||||
| 35 | 566 | ▶ | $self->clear if $key eq 'C'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 60 | ▶ | sub clear { | ||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 39 | 60 | my $self = shift; | |||||||||||||
| 40 | 60 | $self->left(0); | |||||||||||||
| 41 | 60 | $self->right(''); | |||||||||||||
| 42 | 60 | $self->operator('+'); | |||||||||||||
| 43 | 60 | $self->display('0'); | |||||||||||||
| 44 | 60 | $self->equals(''); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 56 | ▶ | sub equalsign { | ||||||||||||
|
Subroutine
equalsign: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 48 | 56 | my $self = shift; | |||||||||||||
| 49 | 56 | $self->key_operator('+'); | |||||||||||||
| 50 | 56 | my $result = $self->left; | |||||||||||||
| 51 | 56 | $self->clear(); | |||||||||||||
| 52 | 56 | $self->equals($result); | |||||||||||||
| 53 | 56 | $self->display($result); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 336 | ▶ | sub digit { | ||||||||||||
|
Subroutine
digit: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||
| 57 | 336 | my ( $self, $digit ) = @_; | |||||||||||||
| 58 | |||||||||||||||
| 59 | # Deal with decimal weirdness | ||||||||||||||
| 60 | 336 | ▶ | if ( $digit eq '.' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 38 | ▶ | return if $self->right =~ m/\./; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 38 | ▶ | $digit = '0.' unless length( $self->right ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 336 | $self->right( $self->right . $digit ); | |||||||||||||
| 66 | 336 | $self->display( $self->right ); | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 152 | ▶ | sub key_operator { | ||||||||||||
|
Subroutine
key_operator: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 70 | 152 | my ( $self, $operator ) = @_; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 152 | ▶ | my $cmd = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 73 | $self->left | ||||||||||||||
| 74 | . $self->operator | ||||||||||||||
| 75 | . ( | ||||||||||||||
| 76 | length( $self->right ) | ||||||||||||||
| 77 | ? $self->right | ||||||||||||||
| 78 | : ( length( $self->equals ) ? $self->equals : '0' ) | ||||||||||||||
| 79 | ); | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 152 | $self->right(''); | |||||||||||||
| 82 | 152 | $self->equals(''); | |||||||||||||
| 83 | |||||||||||||||
| 84 | 152 | $self->left( ( eval $cmd ) + 0 ); | |||||||||||||
| 85 | 152 | $self->display( $self->left ); | |||||||||||||
| 86 | |||||||||||||||
| 87 | 152 | $self->operator($operator); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 3 | 1; | |||||||||||||