aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrage311 <rage_311@hotmail.com>2019-10-25 11:01:22 -0600
committerrage311 <rage_311@hotmail.com>2019-10-25 11:01:22 -0600
commit1ac0290befbff534e6222caa5d745294a31e1509 (patch)
tree0fcc07bea2eee1159f391e9c3a55209130c45a93
parent2adf77af049b3c8fb01c820063088ce142a4e43d (diff)
downloadperlweeklychallenge-club-1ac0290befbff534e6222caa5d745294a31e1509.tar.gz
perlweeklychallenge-club-1ac0290befbff534e6222caa5d745294a31e1509.tar.bz2
perlweeklychallenge-club-1ac0290befbff534e6222caa5d745294a31e1509.zip
Added solutions for 031
-rw-r--r--challenge-031/rage311/perl5/ch-1.pl55
-rw-r--r--challenge-031/rage311/perl5/ch-2.pl21
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-031/rage311/perl5/ch-1.pl b/challenge-031/rage311/perl5/ch-1.pl
new file mode 100644
index 0000000000..cf79ba052f
--- /dev/null
+++ b/challenge-031/rage311/perl5/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# 031-1
+# Create a function to check divide by zero error without checking if the
+# denominator is zero.
+
+use 5.030;
+use strict;
+use warnings;
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+sub divide ($num, $denom) {
+ my $result = eval { $num / $denom };
+ warn "($num / $denom) $@" and return undef if
+ $@ && index($@, 'Illegal division by zero') > -1;
+ return $result;
+}
+
+sub print_div ($num, $denom) {
+ my $result = divide $num, $denom;
+ say "$num / $denom = ", ($result // 'ERROR');
+}
+
+my @tests = (
+ [ 5, 2 ],
+ [ 1, 0 ],
+ [ 99, 999999 ],
+ [ -1.5, 2 ],
+ [ -2, -99999 ],
+ [ 0, 19.991 ],
+ [ 0.123, 0 ],
+ [ 9999999999, 0 ],
+ [ 256, 8 ],
+);
+
+print_div @$_ for @tests;
+
+
+__DATA__
+# Output:
+
+(1 / 0) Illegal division by zero at ch-1.pl line 14.
+(0.123 / 0) Illegal division by zero at ch-1.pl line 14.
+(9999999999 / 0) Illegal division by zero at ch-1.pl line 14.
+5 / 2 = 2.5
+1 / 0 = ERROR
+99 / 999999 = 9.9000099000099e-05
+-1.5 / 2 = -0.75
+-2 / -99999 = 2.0000200002e-05
+0 / 19.991 = 0
+0.123 / 0 = ERROR
+9999999999 / 0 = ERROR
+256 / 8 = 32
+
diff --git a/challenge-031/rage311/perl5/ch-2.pl b/challenge-031/rage311/perl5/ch-2.pl
new file mode 100644
index 0000000000..422602e140
--- /dev/null
+++ b/challenge-031/rage311/perl5/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+# 031-2
+# Create a script to demonstrate creating dynamic variable name, assign a value
+# to the variable and finally print the variable. The variable name would be
+# passed as command line argument.
+
+use 5.030;
+use strict;
+use warnings;
+
+my $var_name = pop;
+eval 'my $' . $var_name . ' = time;'
+ . 'say chr(36), "$var_name = $' . $var_name . '";';
+
+
+__DATA__
+# Output:
+
+$ perl ch-2.pl myvar1
+$myvar1 = 1572022798