aboutsummaryrefslogtreecommitdiff
path: root/challenge-031
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-10-22 14:20:51 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-10-22 14:20:51 +0100
commit8cba287d10540749782a4daee77bccb1c251945a (patch)
tree78a933271de3b52aa9806c7b91939339681d4bb8 /challenge-031
parent97135013a24e8d08fc611211e96eaef751e37ca0 (diff)
downloadperlweeklychallenge-club-8cba287d10540749782a4daee77bccb1c251945a.tar.gz
perlweeklychallenge-club-8cba287d10540749782a4daee77bccb1c251945a.tar.bz2
perlweeklychallenge-club-8cba287d10540749782a4daee77bccb1c251945a.zip
- Added solutions by Burkhard Nickels.
Diffstat (limited to 'challenge-031')
-rw-r--r--challenge-031/burkhard-nickels/perl5/Div.pm126
-rw-r--r--challenge-031/burkhard-nickels/perl5/ch-1.pl35
-rw-r--r--challenge-031/burkhard-nickels/perl5/ch-1a.pl40
-rw-r--r--challenge-031/burkhard-nickels/perl5/ch-2.pl60
4 files changed, 261 insertions, 0 deletions
diff --git a/challenge-031/burkhard-nickels/perl5/Div.pm b/challenge-031/burkhard-nickels/perl5/Div.pm
new file mode 100644
index 0000000000..f7215fc8d6
--- /dev/null
+++ b/challenge-031/burkhard-nickels/perl5/Div.pm
@@ -0,0 +1,126 @@
+#!/usr/bin/perl
+
+package Div;
+
+use strict;
+use warnings;
+use Data::Dumper qw(Dumper);
+require Exporter;
+
+=head1 NAME
+
+Div - Division by Zero Test
+
+=head1 SYNOPSIS
+
+ use Div qw(debug);
+ my $DEBUG=0;
+
+ my $a = Div->new(4);
+ my $b = 2;
+ or
+ my $b = 0;
+ my $r = $a/$b;
+ print "Division $a/$b = $r\n";
+ debug($a,$b,$r) if $DEBUG;
+
+=head1 DESCRIPTION
+
+This is Task#01 of Perl Weekly Challenge #31 / 2019. A division by Zero shall
+be recognized without evaluating the divisor for zero value. Here it is done
+with traping the exception in an C<eval> block. The result (scalar ref) is
+blessed.
+
+ eval { $res = $$self / $divisor };
+ if($@ =~ /division by zero/) { $ret = "Division by zero is not allowed!"; }
+ else { $ret = ref $res ? $res : bless \$res; }
+
+Additionally a class for the division C<Div> is defined and the "/" operator
+is overloaded. A static debug function can be imported, that shows the
+blessing of a scalar ref.
+
+ use overload
+ '/' => \&divide,
+ '""' => \&out,
+ 'fallback' => 1;
+
+=cut
+
+use overload
+ '/' => \&divide,
+ '""' => \&out,
+ 'fallback' => 1;
+
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw(debug);
+$Data::Dumper::Indent=0;
+
+=head2 new() - Constructor
+
+Creates a number as Dividend.
+
+ my $a = Div->new(4);
+
+=cut
+
+sub new {
+ my ($class,$n) = @_;
+ my $self = \$n;
+ bless $self, $class;
+}
+
+=head2 divide() - division
+
+Divides the object with a number.
+
+ my $result = $a->divide(2);
+ or
+ my $result = $a / 2;
+
+=cut
+
+sub divide {
+ my ($self,$divisor) = @_;
+ my ($ret,$res);
+ eval { $res = $$self / $divisor };
+ if($@ =~ /division by zero/) { $ret = "Division by zero is not allowed!"; }
+ else { $ret = ref $res ? $res : bless \$res; }
+ return $ret;
+}
+
+=head2 out() - output
+
+Output of the object. This methode needs to be defined and the '""' needs to
+be overloaded with this function.
+
+Otherwise the output of the number in a print function would not work.
+
+ print "Dividend: $a"
+
+=cut
+
+sub out { my $self = shift; return $$self; }
+
+=head2 debug() - debugging of blessed scalar ref
+
+This is a static function to show the blessing of a scalar reference.
+Because it is outside the object I did an Export on it, that it can be
+used without Class name.
+
+=cut
+
+sub debug {
+ my (@v) = @_;
+ for(my $i=0; $i<=$#v; $i++) {
+ my $w = $v[$i];
+ printf("Value %02d: %-10s (%2s) %s\n",$i,ref($w),$w,Dumper($w));
+ }
+}
+
+=head1 AUTHOR
+
+Chuck
+
+=cut
+
+1;
diff --git a/challenge-031/burkhard-nickels/perl5/ch-1.pl b/challenge-031/burkhard-nickels/perl5/ch-1.pl
new file mode 100644
index 0000000000..216e46d34b
--- /dev/null
+++ b/challenge-031/burkhard-nickels/perl5/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my ($r,$a,$b);
+
+# Division with function divide
+# ------------------------------
+($a,$b) = (5,2);
+$r = divide($a,$b);
+print "Division $a/$b = $r\n";
+
+# Catches the execption and returns message.
+# ------------------------------
+($a,$b) = (4,0);
+$r = divide($a,$b);
+print "Division $a/$b = $r\n";
+
+# Throws an exception
+# ------------------------------
+($a,$b) = (4,0);
+$r = $a/$b;
+print "Division $a/$b = $r\n";
+
+# ------------------- Divide Function ---------------------
+sub divide {
+ my ($dividend,$divisor) = @_;
+ my $result;
+ eval { $result = $dividend/$divisor; };
+ if($@ =~ /division by zero/) { $result = "Division by zero is not allowed!"; }
+ return $result;
+}
+
+
diff --git a/challenge-031/burkhard-nickels/perl5/ch-1a.pl b/challenge-031/burkhard-nickels/perl5/ch-1a.pl
new file mode 100644
index 0000000000..5ad647a8a5
--- /dev/null
+++ b/challenge-031/burkhard-nickels/perl5/ch-1a.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use lib '.';
+use Div qw(debug);
+my $DEBUG=0;
+
+{
+my $a = Div->new(4);
+my $b = 2;
+my $r = $a/$b;
+print "Division $a/$b = $r\n";
+debug($a,$b,$r) if $DEBUG;
+}
+
+{
+my $a = Div->new(4);
+my $b = 0;
+my $r = $a/$b;
+print "Division $a/$b = $r\n";
+debug($a,$b,$r) if $DEBUG;
+}
+
+{
+my $a = Div->new(5);
+my $b = 3;
+my $r = $a/$b;
+print "Division $a/$b = $r\n";
+debug($a,$b,$r) if $DEBUG;
+}
+
+{
+my $a = Div->new(8);
+my $b = 0;
+my $r = $a/$b;
+print "Division $a/$b = $r\n";
+debug($a,$b,$r) if $DEBUG;
+}
+
diff --git a/challenge-031/burkhard-nickels/perl5/ch-2.pl b/challenge-031/burkhard-nickels/perl5/ch-2.pl
new file mode 100644
index 0000000000..d23651c608
--- /dev/null
+++ b/challenge-031/burkhard-nickels/perl5/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use lib '.';
+use Getopt::Long;
+use Div qw(debug);
+my $DEBUG=0;
+
+my ($list,$dividend,$divisor) = (0,"","");
+my $o = GetOptions ("list" => \$list, "dividend=s" => \$dividend, "divisor=s" => \$divisor);
+if(!$o) { usage(); die("Error in command line arguments\n"); }
+
+my %vars = (
+ var4 => 4,
+ var2 => 2,
+ var1 => 1,
+ zero => 0,
+ pi => 3.1417,
+ eternal => 9999.9999,
+);
+
+if($list) { list(); }
+elsif($dividend and $divisor and
+ ($vars{$dividend} or $dividend eq "zero") and
+ ($vars{$divisor} or $divisor eq "zero")) {
+ compute($vars{$dividend},$vars{$divisor});
+}
+else {
+ usage();
+}
+
+# ----------------- Functions ----------------
+sub list {
+ print "List of Vars:\n";
+ foreach my $k (keys %vars) {
+ printf(" %-8s: %5.5f\n", $k, $vars{$k});
+ }
+}
+sub usage {
+ print "Usage: ./pwc312.pl [--list|-l] [--dividend <nr> --divisor <nr>]\n";
+ print " Script is dividing two numbers, dividend/divisor.\n";
+ print " --list, list of possible variables.\n";
+ print " --dividend, dividend for division.\n";
+ print " --divisor, divisor for division.\n";
+ print " Examples:\n";
+ print " ./pwc312.pl -l\n";
+ print " ./pwc312.pl --dividend var1 --divisor var2\n";
+ print " ./pwc312.pl --dividend=var1 --divisor=var3\n";
+}
+
+sub compute {
+ my ($dividend,$divisor) = @_;
+ my $a = Div->new($dividend);
+ my $b = $divisor;
+ my $r = $a/$b;
+ print "Division $a/$b = $r\n";
+ debug($a,$b,$r) if $DEBUG;
+}
+