diff options
| author | PerlMonk Athanasius <PerlMonk.Athanasius@gmail.com> | 2019-10-26 03:05:50 -0700 |
|---|---|---|
| committer | PerlMonk Athanasius <PerlMonk.Athanasius@gmail.com> | 2019-10-26 03:05:50 -0700 |
| commit | 5d074a520edeb492ebb4a213996c0f389b25dafd (patch) | |
| tree | a289fc15e4f850c9d3406dda4837e3a27b2168c8 /challenge-031 | |
| parent | eebcb87b5b57eef54424e41660dc5881e704cf57 (diff) | |
| download | perlweeklychallenge-club-5d074a520edeb492ebb4a213996c0f389b25dafd.tar.gz perlweeklychallenge-club-5d074a520edeb492ebb4a213996c0f389b25dafd.tar.bz2 perlweeklychallenge-club-5d074a520edeb492ebb4a213996c0f389b25dafd.zip | |
Perl 5 & 6 solutions to Tasks 1 & 2 of Challenge #031
On branch branch-for-challenge-031
Changes to be committed:
new file: challenge-031/athanasius/perl5/ch-1.pl
new file: challenge-031/athanasius/perl5/ch-2.pl
new file: challenge-031/athanasius/perl6/ch-1.p6
new file: challenge-031/athanasius/perl6/ch-2.p6
Diffstat (limited to 'challenge-031')
| -rw-r--r-- | challenge-031/athanasius/perl5/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-031/athanasius/perl5/ch-2.pl | 52 | ||||
| -rw-r--r-- | challenge-031/athanasius/perl6/ch-1.p6 | 56 | ||||
| -rw-r--r-- | challenge-031/athanasius/perl6/ch-2.p6 | 46 |
4 files changed, 206 insertions, 0 deletions
diff --git a/challenge-031/athanasius/perl5/ch-1.pl b/challenge-031/athanasius/perl5/ch-1.pl new file mode 100644 index 0000000000..aef3e3a725 --- /dev/null +++ b/challenge-031/athanasius/perl5/ch-1.pl @@ -0,0 +1,52 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 031 +========================= + +Task #1 +------- +Create a function to check *divide by zero error* without checking if the denom- +inator is zero. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Const::Fast; + +const my $DEFAULT_DIVIDEND => 1; +const my $DEFAULT_DIVISOR => 0; + +BEGIN +{ + $| = 1; + print "\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $dividend = $ARGV[0] // $DEFAULT_DIVIDEND; + my $divisor = $ARGV[1] // $DEFAULT_DIVISOR; + my $quotient = 'DIVIDE BY ZERO ERROR'; + + eval + { + $quotient = $dividend / $divisor; + }; + + die $@ if $@ && $@ !~ /^Illegal division by zero/; + + print "$dividend / $divisor = $quotient\n\nNormal exit\n"; +} + +################################################################################ diff --git a/challenge-031/athanasius/perl5/ch-2.pl b/challenge-031/athanasius/perl5/ch-2.pl new file mode 100644 index 0000000000..7ce0c61776 --- /dev/null +++ b/challenge-031/athanasius/perl5/ch-2.pl @@ -0,0 +1,52 @@ +#!perl + +################################################################################ +=comment + +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. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Const::Fast; + +const my $USAGE => "USAGE: perl $0 <Str>\n"; +const my $VALUE => 42; + +BEGIN +{ + $| = 1; + print "\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + scalar @ARGV == 1 or die $USAGE; + + my $variable_name = $ARGV[0]; + + { + no strict qw( refs ); + + $$variable_name = $VALUE; + + print "\$$variable_name = $$variable_name\n"; + } +} + +################################################################################ diff --git a/challenge-031/athanasius/perl6/ch-1.p6 b/challenge-031/athanasius/perl6/ch-1.p6 new file mode 100644 index 0000000000..59af62c2f8 --- /dev/null +++ b/challenge-031/athanasius/perl6/ch-1.p6 @@ -0,0 +1,56 @@ +use v6; + +################################################################################ +=begin comment + +Perl Weekly Challenge 031 +========================= + +Task #1 +------- +Create a function to check *divide by zero error* without checking if the denom- +inator is zero. + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +my Real constant $DEFAULT-DIVIDEND = 1; +my Real constant $DEFAULT-DIVISOR = 0; + +BEGIN say ''; + +#=============================================================================== +sub MAIN +( + Real:D $dividend = $DEFAULT-DIVIDEND, + Real:D $divisor = $DEFAULT-DIVISOR, +) +#=============================================================================== +{ + try + { + my Real $quotient = $dividend / $divisor; + + "$dividend / $divisor = $quotient".say; + } + + if $! + { + if $! ~~ rx/ ^ Attempt \s to \s divide .+ by \s zero / + { + "$dividend / $divisor = DIVIDE BY ZERO ERROR".say; + } + else + { + $!.throw; + } + } + + "\nNormal exit".say; +} + +################################################################################ diff --git a/challenge-031/athanasius/perl6/ch-2.p6 b/challenge-031/athanasius/perl6/ch-2.p6 new file mode 100644 index 0000000000..dd2144e853 --- /dev/null +++ b/challenge-031/athanasius/perl6/ch-2.p6 @@ -0,0 +1,46 @@ +use v6; + +################################################################################ +=begin comment + +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. + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use MONKEY-SEE-NO-EVAL; + +my Real constant $VALUE = 42; + +BEGIN say ''; + +#=============================================================================== +sub MAIN(Str:D $variable-name) +#=============================================================================== +{ + # Declare the variable and assign a value to it + + my Str $expression = "my \$$variable-name = $VALUE;"; + + # Print the variable + + $expression ~= " qq[\\\$$variable-name = \$$variable-name].say;"; + + # Declaration, assignment, and printing must be EVALuated together to avoid + # a "Variable ... is not declared" error in the say statement + + EVAL $expression; +} + +################################################################################ |
