diff options
| author | mark-senn <mark@senn.us> | 2019-10-27 15:39:12 -0400 |
|---|---|---|
| committer | mark-senn <mark@senn.us> | 2019-10-27 15:39:12 -0400 |
| commit | ea21bfb4c704fa01d8335bd3244a2e930c6550dd (patch) | |
| tree | bf0c50a5041532bbfa4826434b90a6069ac8b711 /challenge-031/mark-senn | |
| parent | b06bfa34831dcbd0f6211c4a07b4cb0d8450d94a (diff) | |
| download | perlweeklychallenge-club-ea21bfb4c704fa01d8335bd3244a2e930c6550dd.tar.gz perlweeklychallenge-club-ea21bfb4c704fa01d8335bd3244a2e930c6550dd.tar.bz2 perlweeklychallenge-club-ea21bfb4c704fa01d8335bd3244a2e930c6550dd.zip | |
mark-senn adding 031
Diffstat (limited to 'challenge-031/mark-senn')
| -rw-r--r-- | challenge-031/mark-senn/perl6/ch-1.p6 | 82 | ||||
| -rw-r--r-- | challenge-031/mark-senn/perl6/ch-2.p6 | 40 |
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-031/mark-senn/perl6/ch-1.p6 b/challenge-031/mark-senn/perl6/ch-1.p6 new file mode 100644 index 0000000000..294fe9f309 --- /dev/null +++ b/challenge-031/mark-senn/perl6/ch-1.p6 @@ -0,0 +1,82 @@ +# +# Perl Weekly Challenge - 031 +# Task #1 +# +# Mark Senn, http://engineering.purdue.edu/~mark +# October 25, 2019 +# +# From +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-031#task-1 +# Create a function to check divide by zero error +# without checking if the denominator is zero. +# +# Below are three ways to do this. +# + +# Perl 6 is in the process of being renamed Raku. +# Run using Raku v6.d; +use v6.d; + + +# Return $c or, if $c is undefined return Inf and let +# the caller handle that. +sub div1($a, $b) { + my $c = $a / $b; + return $c // Inf; +} + + +# Raise a "/0 or other problem" exception. I'm using "or other problem" +# in case something else (maybe a bug) causes $c to be Inf. +# This code is based on code from +# https://docs.perl6.org/type/Failure +sub div2($a, $b) { + my $c = $a / $b; + ($c == Inf) and fail '/0 or other problem'; + return $c; +} + + +sub div3($a, $b) { + my $c; + try { + CATCH { + when (X::Numeric::DivideByZero) { return ' /0 or other problem'; } + } + $c = $a / $b; + } + return " $c"; +} + + +sub MAIN() { + + my $n = 10; + + print 'div1:'; + for (0,2) -> $d { + my $c = div1($n, $d); + ($c == Inf) + ?? ' /0 or other problem'.print + !! " $c".say; + } + + print 'div2:'; + for (0,2) -> $d { + with div2($n, $d) -> $c { + " $c".say; + } else { + " {.exception.message}".print; + } + } + + print 'div3:'; + for (0,2) -> $d { + # This won't work without using ".Num". + my $c = div3($n.Num, $d.Num); + $c.print; + } + ''.say; + +} + diff --git a/challenge-031/mark-senn/perl6/ch-2.p6 b/challenge-031/mark-senn/perl6/ch-2.p6 new file mode 100644 index 0000000000..6ee3ff2722 --- /dev/null +++ b/challenge-031/mark-senn/perl6/ch-2.p6 @@ -0,0 +1,40 @@ +# +# Perl Weekly Challenge - 031 +# Task #2 +# +# Mark Senn, http://engineering.purdue.edu/~mark +# October 25, 2019 +# +# From +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-031#task-2 +# 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. +# +# Perl 6 is in the process of being renamed Raku. +# Run using Raku v6.d; +use v6.d; + +sub MAIN($name, $value) +{ + say "$name $value"; + + # Using + # my $$name = $value; + # gave + # Cannot declare a variable by indirect name (use a hash instead?) + # + # Using + # ${$name} = $value; + # gave + # Unsupported use of ${$name}; in Perl 6 please use $($name) for hard ref + # or $::($name) for symbolic ref + # + # Using + # my $::($name); + # gave + # Cannot declare a variable by indirect name (use a hash instead?) + my %hash; + %hash{$name} = $value; + %hash{$name}.say; +} |
