aboutsummaryrefslogtreecommitdiff
path: root/challenge-034/yet-ebreo/perl5/ch-2.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-034/yet-ebreo/perl5/ch-2.pl')
-rw-r--r--challenge-034/yet-ebreo/perl5/ch-2.pl41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-034/yet-ebreo/perl5/ch-2.pl b/challenge-034/yet-ebreo/perl5/ch-2.pl
new file mode 100644
index 0000000000..b0c253f846
--- /dev/null
+++ b/challenge-034/yet-ebreo/perl5/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+my %table = (
+ add => \&add,
+ subtract => \&subtract,
+ multiply => \&multiply,
+ divide => \&divide
+);
+
+sub divide {
+ my ($m,$n) = @_;
+ say "Error: Divide by 0" if !$n;
+
+ return $m/$n;
+}
+
+sub add {
+ my ($m,$n) = @_;
+ return $m+$n;
+}
+
+sub subtract {
+ my ($m,$n) = @_;
+ return $m-$n;
+}
+
+sub multiply {
+ my ($m,$n) = @_;
+ return $m*$n;
+}
+
+my $commands = "add multiply subtract divide";
+my @operands = (43,6);
+for my $cmd ($commands=~/\w+/g) {
+ say "[$cmd]";
+ say $table{$cmd}->(@operands);
+} \ No newline at end of file