my $numA= $ARGV[0]; #5
my $numB= $ARGV[1]; #5
my $func= $ARGV[2]; #+
my $result = $numA .$func . $numB; #The result shoudl be 10
print "The result is: $result"; # The result is: 5+5
I have this code and I want to get '$func' as an operator to add '$numA' with '$numB'.
It's convenient that the expression is already parsed.
my %ops = (
'+' => sub { $_[0] + $_[1] },
'-' => sub { $_[0] - $_[1] },
'*' => sub { $_[0] * $_[1] },
'/' => sub { $_[0] / $_[1] },
);
my ($operand1, $operand2, $operator) = @ARGV;
say $ops{$operator}->($operand1, $operand2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With