aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Thegler <lth@fibia.dk>2019-10-24 21:00:20 +0200
committerLars Thegler <lth@fibia.dk>2019-10-24 21:00:20 +0200
commitf98f7cf1f7d71b0c68804d2edc397c817e58a907 (patch)
tree9896a5b0882a9daac705656ab72927f9ef92f1eb
parent81c5875659fcfd79c885094d2a09517c06093cf4 (diff)
downloadperlweeklychallenge-club-f98f7cf1f7d71b0c68804d2edc397c817e58a907.tar.gz
perlweeklychallenge-club-f98f7cf1f7d71b0c68804d2edc397c817e58a907.tar.bz2
perlweeklychallenge-club-f98f7cf1f7d71b0c68804d2edc397c817e58a907.zip
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