diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-07-22 22:01:29 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-07-25 09:09:50 +0200 |
| commit | 250f908839ec00b0f1b9d0480056481a0d3a2e90 (patch) | |
| tree | 6c05ae68d6ec115ab56a77475bb92bf2ea77f6e1 | |
| parent | 864b54240022daa9c51f4d30e531f1950d3d348c (diff) | |
| download | perlweeklychallenge-club-250f908839ec00b0f1b9d0480056481a0d3a2e90.tar.gz perlweeklychallenge-club-250f908839ec00b0f1b9d0480056481a0d3a2e90.tar.bz2 perlweeklychallenge-club-250f908839ec00b0f1b9d0480056481a0d3a2e90.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-331/jo-37/perl/ch-1.pl | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/challenge-331/jo-37/perl/ch-1.pl b/challenge-331/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..389838b6a0 --- /dev/null +++ b/challenge-331/jo-37/perl/ch-1.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - length of last word + + usage: $0 [-examples] [-tests] [STR...] + + -examples + run the examples from the challenge + + -tests + run some tests + + STR... + string or list of words + + EOS +} + + +### Input and Output + +say llw("@ARGV"); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/07/25/ch-331.html#task-1 + + +sub llw { + () = shift =~ /\w+/g or return 0; + $+[0] - $-[0]; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = llw(@$args); + is $result, $expected, + qq($name: "@$args" -> $expected); + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["The Weekly Challenge"], 9, 'example 1'], + [[" Hello World "], 5, 'example 2'], + [["Let's begin the fun"], 3, 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 2; + is llw('... --- +++ /// '), 0, 'no word'; + is llw("it isn't"), 1, 'part only'; + }) : pass 'skip tests'; + + exit; +} |
