aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-10-22 17:21:27 -0300
committerandrezgz <andrezgz@gmail.com>2019-10-22 17:21:27 -0300
commit23b2902e009d9188c75a90c5108e7bc74ec1cdc1 (patch)
tree0105bc054472ba3a16da3f6a918e6e7eef5837b0
parent20a4db6a585d62ff2a5516efc0c0ca49934530f5 (diff)
downloadperlweeklychallenge-club-23b2902e009d9188c75a90c5108e7bc74ec1cdc1.tar.gz
perlweeklychallenge-club-23b2902e009d9188c75a90c5108e7bc74ec1cdc1.tar.bz2
perlweeklychallenge-club-23b2902e009d9188c75a90c5108e7bc74ec1cdc1.zip
challenge-031 andrezgz solution
-rw-r--r--challenge-031/andrezgz/perl5/ch-1.pl17
-rw-r--r--challenge-031/andrezgz/perl5/ch-2.pl17
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-031/andrezgz/perl5/ch-1.pl b/challenge-031/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..3da84bdbb8
--- /dev/null
+++ b/challenge-031/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+
+# 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.
+
+use strict;
+use warnings;
+
+sub is_division_by_zero {
+ my ($num,$den) = @_;
+ return if ( eval { $num = $num/$den; 1 } );
+ return 1;
+}
+
+print 'Division by zero' if is_division_by_zero(1,0);
diff --git a/challenge-031/andrezgz/perl5/ch-2.pl b/challenge-031/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..d17492f8f4
--- /dev/null
+++ b/challenge-031/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+
+# 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.
+
+use strict;
+use warnings;
+
+my $var_name = shift;
+
+# It's gettin' ugly
+no strict 'refs'; # This works sometimes, but it's a very bad idea.
+${$var_name} = 'a value'; # For reasons and better alternatives refer to
+print 'the variable' if ${$var_name} eq 'a value'; # https://metacpan.org/pod/perlfaq7#How-can-I-use-a-variable-as-a-variable-name