aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-21 13:27:51 +0100
committerGitHub <noreply@github.com>2019-10-21 13:27:51 +0100
commit2ca36a2e42c0ffafddfbbdc17da1732a134ec5d9 (patch)
treef577082df759457692de050e7f46633933a490c9
parente106eb7eaa5349c9722b0892964ef0ecc7838577 (diff)
parent38341a4d5e342925bf1674f788940b140ed2b696 (diff)
downloadperlweeklychallenge-club-2ca36a2e42c0ffafddfbbdc17da1732a134ec5d9.tar.gz
perlweeklychallenge-club-2ca36a2e42c0ffafddfbbdc17da1732a134ec5d9.tar.bz2
perlweeklychallenge-club-2ca36a2e42c0ffafddfbbdc17da1732a134ec5d9.zip
Merge pull request #820 from Doomtrain14/master
Added perl5 solution ch#31-2
-rw-r--r--challenge-031/yet-ebreo/perl5/ch-2.pl34
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-031/yet-ebreo/perl5/ch-2.pl b/challenge-031/yet-ebreo/perl5/ch-2.pl
new file mode 100644
index 0000000000..92895a9f46
--- /dev/null
+++ b/challenge-031/yet-ebreo/perl5/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+# 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 feature 'say';
+
+die "Usage:\n\tperl ch-2.pl <variable name>\n\n" if @ARGV != 1;
+die "Error: Only alphanumeric characters and underscore are allowed\n\n" if $ARGV[0]=~/\W+/;
+
+# Not exactly what the task is asking for
+# but it still demonstrates that a variable can be
+# dynamically created
+
+# The basic idea here is that if the variable can be referenced
+# then the variable exists
+
+# I used eval to basically do
+# my $varname = \$<some_variable_name>; of course with out <>
+# This would create a reference to a scalar value
+my $varname = eval("\\\$" . $ARGV[0]);
+
+# Changes to the reference...
+$$varname = 0|100*rand;
+
+# Should update the value of the variable being referred to
+# the code below will print the random generated value assigned
+# to the reference *IF* the user inputs "dynamic_var" as
+# the variable name, otherwise the variable will be empty
+
+say $dynamic_var;
+
+#NOTE: because of the nature of the task,
+# use strict was removed.