diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-12 22:26:54 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-23 18:03:40 +0100 |
| commit | bdb872733ea2328d22df0bf5ebcfcbad04cd84da (patch) | |
| tree | 31dc901b76e0511a712f25e6cbcdd9b2ceb4bad3 | |
| parent | ff260192480fda25385374aedf44a2d672002acd (diff) | |
| download | perlweeklychallenge-club-bdb872733ea2328d22df0bf5ebcfcbad04cd84da.tar.gz perlweeklychallenge-club-bdb872733ea2328d22df0bf5ebcfcbad04cd84da.tar.bz2 perlweeklychallenge-club-bdb872733ea2328d22df0bf5ebcfcbad04cd84da.zip | |
Challenge 008 task 2
| -rwxr-xr-x | challenge-008/jo-37/perl/ch-2.pl | 56 |
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; +} |
