aboutsummaryrefslogtreecommitdiff
path: root/challenge-272
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-06-05 19:08:55 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-06-07 22:48:42 +0200
commit3e4dc5350b29cea68cb7f32562e97463722ba1c0 (patch)
tree214bc9780bd20d5e3d1a48741c586b678f5f8d76 /challenge-272
parent48d0e34525b4183b4943fd8108e14755bf96e6d4 (diff)
downloadperlweeklychallenge-club-3e4dc5350b29cea68cb7f32562e97463722ba1c0.tar.gz
perlweeklychallenge-club-3e4dc5350b29cea68cb7f32562e97463722ba1c0.tar.bz2
perlweeklychallenge-club-3e4dc5350b29cea68cb7f32562e97463722ba1c0.zip
Solution to task 1
Diffstat (limited to 'challenge-272')
-rwxr-xr-xchallenge-272/jo-37/perl/ch-1.pl65
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-272/jo-37/perl/ch-1.pl b/challenge-272/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..9ec3849c1b
--- /dev/null
+++ b/challenge-272/jo-37/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use Regexp::Common 'net';
+
+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
+ "defang" string
+
+EOS
+
+
+### Input and Output
+
+say defang(shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/07/ch-272.html#task-1
+
+
+sub defang {
+ return /^$RE{net}{IPv4}$/ &&
+
+ s([.])([.])gr
+
+ for shift;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is defang("1.1.1.1"), "1[.]1[.]1[.]1", "example 1";
+ is defang("255.101.1.0"), "255[.]101[.]1[.]0", "example 2";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is defang("256.0.0.1"), F(), 'invalid address';
+ is defang("1.0.0.256"), F(), 'invalid address';
+ }
+
+ done_testing;
+ exit;
+}