aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-14 13:51:32 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-18 16:50:55 +0200
commitc428cf2c1c04957a897efcca23123c2d2d6bafd9 (patch)
treebfa592b8507a09c91256a4721dd4f07d977fc266
parent8593bc8aa1b11da04cca963c54e2430b8be41bff (diff)
downloadperlweeklychallenge-club-c428cf2c1c04957a897efcca23123c2d2d6bafd9.tar.gz
perlweeklychallenge-club-c428cf2c1c04957a897efcca23123c2d2d6bafd9.tar.bz2
perlweeklychallenge-club-c428cf2c1c04957a897efcca23123c2d2d6bafd9.zip
Solution to task 2
-rwxr-xr-xchallenge-330/jo-37/perl/ch-2.pl96
1 files changed, 96 insertions, 0 deletions
diff --git a/challenge-330/jo-37/perl/ch-2.pl b/challenge-330/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..cea49d1a22
--- /dev/null
+++ b/challenge-330/jo-37/perl/ch-2.pl
@@ -0,0 +1,96 @@
+#!/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 - title capital
+
+ usage: $0 [-examples] [-tests] [WORD...]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ WORD...
+ list of words or strings
+
+ EOS
+}
+
+
+### Input and Output
+
+say title_capital("@ARGV");
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/07/18/ch-330.html#task-2
+
+
+sub title_capital {
+ lc(shift) =~ s/\w{3,}/\u$&/gr;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = title_capital(@$args);
+ is $result, $expected,
+ "$name: '@$args' -> '$expected'";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["PERL IS gREAT"], "Perl is Great" , 'example 1'],
+ [["THE weekly challenge"], "The Weekly Challenge", 'example 2'],
+ [["YoU ARE A stAR"], "You Are a Star" , 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [["LJETo sUMMER sommer"], "Ljeto Summer Sommer",
+ 'titlecase digraph'],
+ [["ÜBLER ärger mit Öl"], "Übler Ärger Mit öl" , 'Umlauts'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}