aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-22 20:17:39 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-22 20:19:41 +0200
commit3c6a2112dce43f4eb4119b0d257d6c66de0c5393 (patch)
tree95f4265e77a434e400d17c6996f0a64275d3941c
parent7d0a73e6eab06187e61b16b59726f4b2798527cd (diff)
downloadperlweeklychallenge-club-3c6a2112dce43f4eb4119b0d257d6c66de0c5393.tar.gz
perlweeklychallenge-club-3c6a2112dce43f4eb4119b0d257d6c66de0c5393.tar.bz2
perlweeklychallenge-club-3c6a2112dce43f4eb4119b0d257d6c66de0c5393.zip
Solution to task 2
-rwxr-xr-xchallenge-287/jo-37/perl/ch-2.pl65
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-287/jo-37/perl/ch-2.pl b/challenge-287/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..27124c11b7
--- /dev/null
+++ b/challenge-287/jo-37/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use Regexp::Common;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STR
+ a string
+
+EOS
+
+
+### Input and Output
+
+say +(qw(true false))[!valid_number(shift)];
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/09/22/ch-287.html#task-2
+
+
+sub valid_number {
+ shift =~ /^$RE{num}{real}\z/;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ ok valid_number("1"), "example 1";
+ ok !valid_number("a"), "example 2";
+ ok !valid_number("."), "example 3";
+ ok !valid_number("1.2e4.2"), "example 4";
+ ok valid_number("-1."), "example 5";
+ ok valid_number("+1E-8"), "example 6";
+ ok valid_number(".44"), "example 7";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ ok !valid_number("42\n"), "newline not allowed";
+ }
+
+ done_testing;
+ exit;
+}