aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-008/jo-37/perl/ch-2.pl56
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-008/jo-37/perl/ch-2.pl b/challenge-008/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..6523dea489
--- /dev/null
+++ b/challenge-008/jo-37/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Util 'max';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STRING...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STRING...
+ strings to be centered
+
+EOS
+
+
+### Input and Output
+
+say for center(@ARGV);
+
+
+### Implementation
+
+sub center {
+ my $max = max map length, @_;
+ map ' ' x (($max - length)/2) . $_, @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [center("This", "is", "a test of the", "center function")],
+ [" This", " is", " a test of the", "center function"],
+ 'example';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}