aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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