I have a script that allows the user to input the following:
EQUALS
CLEAR
PLUS
MINUS
OVER
TIMES
But I run the script then type PLUS 42, it actually makes the value 43.
Please help
#!/usr/bin/perl
use strict;
use warnings;
my $value;
print "\n> ";
while ( <> ) {
chomp;
if ( /^CLEAR$/ ) { $value = 0; }
elsif ( /^EQUALS$/ ) {
if ( defined $value ) {
print "\n = $value\n";
}
else { print "\n (undefined)\n"; }
}
elsif (defined $value && /^PLUS|^MINUS|^TIMES|^OVER/ ) {
$value = compute($value);
}
else {
print "\nInvalid Statement\n";
}
print "\nOK\n";
print "\n> ";
}
sub compute {
my $num = @_;
print "\nNum is: $num\n";
if ( /^PLUS\s([0-9]+)/ ) {
$num += $1;
}
elsif ( /^MINUS\s([0-9]+)/ ) {
$num -= $1;
}
elsif ( /^TIMES\s([0-9]+)/ ) {
$num *= $1;
}
elsif ( /^OVER\s([0-9]+)/ ) {
$num /= $1;
}
return $num;
}
EQUALS
CLEAR
PLUS
MINUS
OVER
TIMES
But I run the script then type PLUS 42, it actually makes the value 43.
Please help
#!/usr/bin/perl
use strict;
use warnings;
my $value;
print "\n> ";
while ( <> ) {
chomp;
if ( /^CLEAR$/ ) { $value = 0; }
elsif ( /^EQUALS$/ ) {
if ( defined $value ) {
print "\n = $value\n";
}
else { print "\n (undefined)\n"; }
}
elsif (defined $value && /^PLUS|^MINUS|^TIMES|^OVER/ ) {
$value = compute($value);
}
else {
print "\nInvalid Statement\n";
}
print "\nOK\n";
print "\n> ";
}
sub compute {
my $num = @_;
print "\nNum is: $num\n";
if ( /^PLUS\s([0-9]+)/ ) {
$num += $1;
}
elsif ( /^MINUS\s([0-9]+)/ ) {
$num -= $1;
}
elsif ( /^TIMES\s([0-9]+)/ ) {
$num *= $1;
}
elsif ( /^OVER\s([0-9]+)/ ) {
$num /= $1;
}
return $num;
}