aboutsummaryrefslogtreecommitdiff
path: root/challenge-112/polettix/perl/ch-1.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-112/polettix/perl/ch-1.pl')
-rw-r--r--challenge-112/polettix/perl/ch-1.pl28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-112/polettix/perl/ch-1.pl b/challenge-112/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..a38693a166
--- /dev/null
+++ b/challenge-112/polettix/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+use Test::More;
+
+sub canonical_path ($p) {
+ $p =~ s{/\K(?:\.?/)+}{}gmxs;
+ $p =~ s{\A/.*\K/\z}{}mxs;
+ 1 while $p =~ s{/[^/]+/\.\.(/|\z)}{$1}mxs;
+ return $p;
+}
+
+for my $test(
+ [qw< /a/ /a >],
+ [qw< /a//b/c/ /a/b/c >],
+ [qw< /a/b/c/../.. /a >],
+ [qw< /a/b/c/../../ /a >],
+ [qw< /a/./b/.//./c/../../ /a >],
+ [qw< /a/../../../b/ /b >],
+) {
+ my ($input, $expected) = $test->@*;
+ my $got = canonical_path($input);
+ is $got, $expected, "'$input' -> '$expected'";
+}
+
+done_testing;