From d3b3f1332289bf3ea29fd7b46c6005110f4c99b1 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Thu, 24 Oct 2019 22:55:57 +0200 Subject: Add solutions to 031 (Division by zero + Dynamic variable name) by E. Choroba --- challenge-031/e-choroba/perl5/ch-1.pl | 22 +++++++++++++++++++++ challenge-031/e-choroba/perl5/ch-2.pl | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 challenge-031/e-choroba/perl5/ch-1.pl create mode 100755 challenge-031/e-choroba/perl5/ch-2.pl diff --git a/challenge-031/e-choroba/perl5/ch-1.pl b/challenge-031/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..5409d28723 --- /dev/null +++ b/challenge-031/e-choroba/perl5/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Try::Tiny; + +sub division { + my ($numerator, $denominator) = @_; + try { + { safe => 1, result => $numerator / $denominator } + } catch { + { safe => 0, error => $_ } + } +} + +use Test::More tests => 4; + +ok division(1,2)->{safe}; +ok !division(1,0)->{safe}; + +is division(1,2)->{result}, 1/2; +like division(1,0)->{error}, qr/Illegal division by zero/; diff --git a/challenge-031/e-choroba/perl5/ch-2.pl b/challenge-031/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..ca9c5dc731 --- /dev/null +++ b/challenge-031/e-choroba/perl5/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +use warnings; +use strict; + +my $var_name = shift; +{ no strict; + ${ $var_name } = "Don't try this at home!"; + print "\$$var_name: ", $$var_name, "\n"; +} + +=head1 Some interesting values + +=over + +=item ! or ? + +Argument "Don't try this at home!" isn't numeric in scalar assignment + +=item & or + + +Modification of a read-only value attempted + +=item \ + + Don't try this at home! + Don't try this at home! + +No newline after the second line. + + +=item , + +Don't try this at home!Don't try this at home!Don't try this at home! + +=back + +=cut -- cgit