aboutsummaryrefslogtreecommitdiff
path: root/challenge-107
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-06 17:35:11 +0200
committerAbigail <abigail@abigail.be>2021-04-06 17:35:11 +0200
commitd9e6a738049b9f95eee9daec882f04e60e82a609 (patch)
tree153a2ccd57afca967a5746471b17a0052367fc56 /challenge-107
parent9941d85738c02e09f56b1479b94e008b0af8e1c0 (diff)
downloadperlweeklychallenge-club-d9e6a738049b9f95eee9daec882f04e60e82a609.tar.gz
perlweeklychallenge-club-d9e6a738049b9f95eee9daec882f04e60e82a609.tar.bz2
perlweeklychallenge-club-d9e6a738049b9f95eee9daec882f04e60e82a609.zip
Perl solution for week 107, part 2
Diffstat (limited to 'challenge-107')
-rw-r--r--challenge-107/abigail/README.md1
-rw-r--r--challenge-107/abigail/perl/ch-2.pl59
-rw-r--r--challenge-107/abigail/t/Calc.pm11
-rw-r--r--challenge-107/abigail/t/ctest.ini6
-rw-r--r--challenge-107/abigail/t/input-2-10
-rw-r--r--challenge-107/abigail/t/output-2-1.exp5
6 files changed, 82 insertions, 0 deletions
diff --git a/challenge-107/abigail/README.md b/challenge-107/abigail/README.md
index e7eb5e02bd..ffb6203bbc 100644
--- a/challenge-107/abigail/README.md
+++ b/challenge-107/abigail/README.md
@@ -88,6 +88,7 @@ add
~~~~
### Solutions
+* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-107/abigail/perl/ch-2.pl b/challenge-107/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..a3194ba7de
--- /dev/null
+++ b/challenge-107/abigail/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-2.pl module
+#
+# where 'module' is the name of the module to be inspected, and
+# this module is to be found in a file named 'module.pm' (with ::
+# replaced by '/'), somewhere in @INC.
+#
+
+#
+# Get the module name from the command line.
+#
+my $module = shift;
+
+#
+# Load the module.
+#
+eval "use $module; 1" or die "Failed to load $module.pm: $@";
+
+#
+# Create a reference to the symbol table of the module.
+#
+my $symbol_table = do {no strict 'refs'; \%{$module . "::"}};
+
+#
+# Iterate over the symbols in the name space, and print out
+# which symbols have a code ref entry in the type glob.
+# (The symbol table maps symbols to type globs).
+#
+foreach my $symbol (keys %$symbol_table) {
+ say $symbol if *{$$symbol_table {$symbol}} {CODE};
+}
+
+#
+# Print BEGIN. This is not a method. While there is a entry
+# named "BEGIN" in the symbol table, the type glob does not
+# have CODE entry. $module -> can ("BEGIN") fails as well.
+#
+# We only print "BEGIN" so we pass the one test given for
+# this exercise.
+#
+# We will *NOT* do the same for END, INIT, CHECK, nor UNITCHECK.
+# One piece of madness is enough.
+#
+say "BEGIN" if $$symbol_table {BEGIN};
diff --git a/challenge-107/abigail/t/Calc.pm b/challenge-107/abigail/t/Calc.pm
new file mode 100644
index 0000000000..4123090fde
--- /dev/null
+++ b/challenge-107/abigail/t/Calc.pm
@@ -0,0 +1,11 @@
+package Calc;
+
+use strict;
+use warnings;
+
+sub new { bless {}, shift; }
+sub add { }
+sub mul { }
+sub div { }
+
+1;
diff --git a/challenge-107/abigail/t/ctest.ini b/challenge-107/abigail/t/ctest.ini
index 2b2795a72b..3a493dec84 100644
--- a/challenge-107/abigail/t/ctest.ini
+++ b/challenge-107/abigail/t/ctest.ini
@@ -1,8 +1,14 @@
[names]
1-1 = Fixed output
+2-1 = Given example
[1-1]
no_input = 1
[1-1/sed]
no_input = 0
+
+[2-1/perl]
+exe_args = -It
+args = %RUN_FILE Calc
+sort = 1
diff --git a/challenge-107/abigail/t/input-2-1 b/challenge-107/abigail/t/input-2-1
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-107/abigail/t/input-2-1
diff --git a/challenge-107/abigail/t/output-2-1.exp b/challenge-107/abigail/t/output-2-1.exp
new file mode 100644
index 0000000000..fdb52d0469
--- /dev/null
+++ b/challenge-107/abigail/t/output-2-1.exp
@@ -0,0 +1,5 @@
+BEGIN
+add
+div
+mul
+new