aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-25 01:28:53 +0100
committerGitHub <noreply@github.com>2019-10-25 01:28:53 +0100
commit2adf77af049b3c8fb01c820063088ce142a4e43d (patch)
tree0108b0111a39a5290b6850dd53a763793b33cfcb
parent5562d170608f1653c47b1f798a52f2bceeb148fe (diff)
parentf98f7cf1f7d71b0c68804d2edc397c817e58a907 (diff)
downloadperlweeklychallenge-club-2adf77af049b3c8fb01c820063088ce142a4e43d.tar.gz
perlweeklychallenge-club-2adf77af049b3c8fb01c820063088ce142a4e43d.tar.bz2
perlweeklychallenge-club-2adf77af049b3c8fb01c820063088ce142a4e43d.zip
Merge pull request #834 from tagg/branch-for-challenge-031
solutions to challenge 031
-rwxr-xr-xchallenge-031/lars-thegler/perl5/ch-1.pl27
-rwxr-xr-xchallenge-031/lars-thegler/perl5/ch-2.pl14
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-031/lars-thegler/perl5/ch-1.pl b/challenge-031/lars-thegler/perl5/ch-1.pl
new file mode 100755
index 0000000000..429ead5eac
--- /dev/null
+++ b/challenge-031/lars-thegler/perl5/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use Modern::Perl;
+use Try::Tiny;
+
+# Create a function to check divide by zero error without checking
+# if the denominator is zero.
+
+say divide_by_zero_error( 4, 0 );
+say divide_by_zero_error( 3, 12 );
+say divide_by_zero_error( 0, 4 );
+
+sub divide_by_zero_error
+{
+ my ( $numerator, $denominator ) = @_;
+ my $error;
+ try {
+ my $ratio = $numerator / $denominator;
+ } catch {
+ if ($_ =~ /division by zero/) {
+ $error = 'divide by zero error';
+ } else {
+ die $_;
+ }
+ };
+ return $error // 'all good';
+}
diff --git a/challenge-031/lars-thegler/perl5/ch-2.pl b/challenge-031/lars-thegler/perl5/ch-2.pl
new file mode 100755
index 0000000000..03bd6adf13
--- /dev/null
+++ b/challenge-031/lars-thegler/perl5/ch-2.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+
+use Modern::Perl;
+
+# 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.
+
+my $variable_name = shift or die 'missing variable name';
+
+no strict 'refs';
+$$variable_name = 42;
+
+say "name=$variable_name, value=$$variable_name"; \ No newline at end of file