From 2f70f5f3a8fc69bf5d2c4aa0dc823cfe3c6d0a16 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sat, 26 Oct 2019 18:46:28 -0600 Subject: Joelle's solutions in Raku and Perl to 31.1 --- challenge-031/joelle-maslak/perl5/ch-1.pl | 33 +++++++++++++++++++++++++++++++ challenge-031/joelle-maslak/perl6/ch-1.p6 | 28 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 challenge-031/joelle-maslak/perl5/ch-1.pl create mode 100755 challenge-031/joelle-maslak/perl6/ch-1.p6 diff --git a/challenge-031/joelle-maslak/perl5/ch-1.pl b/challenge-031/joelle-maslak/perl5/ch-1.pl new file mode 100755 index 0000000000..09afa38e3a --- /dev/null +++ b/challenge-031/joelle-maslak/perl5/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.24; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +if ( @ARGV != 2 ) { die("Provide letters to use and (optionally) filename") } + +MAIN: { + if ( check_for_div_by_zero(@ARGV) ) { + say "Denominator is zero"; + } else { + say "Denominator is not zero"; + } +} + +sub check_for_div_by_zero ( $top, $bottom ) { + local $@; + my $result = + eval { use warnings FATAL => 'all'; $top / $bottom }; # Make things like "1 / abc" fatal. + + if ( defined($result) ) { + return; + } elsif ( $@ =~ /^Illegal division by zero / ) { + return 1; + } else { + die($@); + } +} + diff --git a/challenge-031/joelle-maslak/perl6/ch-1.p6 b/challenge-031/joelle-maslak/perl6/ch-1.p6 new file mode 100755 index 0000000000..7d2b97aadf --- /dev/null +++ b/challenge-031/joelle-maslak/perl6/ch-1.p6 @@ -0,0 +1,28 @@ +#!/usr/bin/env perl6 +use v6; + +# +# Copyright © 2019 Joelle Maslak +# All Rights Reserved - See License +# + +sub MAIN($numerator, $denominator) { + if test-for-div-by-zero($numerator, $denominator) { + say "Denominator is zero"; + } else { + say "Denominator is not zero"; + } +} + +sub test-for-div-by-zero($numerator, $denominator) { + ($numerator / $denominator).Int.sink; + + return; # Not div by zero + + CATCH { + when X::Numeric::DivideByZero { + return 1; # Div by zero + } + } +} + -- cgit