aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-15 19:32:08 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-23 18:03:41 +0100
commit51839d5ae944b3fb4c880f825050e0317067b8e9 (patch)
treedb84414ca59dbc2cc8f8b886790323250479d5a6
parent27746120072266d19b4aba4970e75098b6995b7d (diff)
downloadperlweeklychallenge-club-51839d5ae944b3fb4c880f825050e0317067b8e9.tar.gz
perlweeklychallenge-club-51839d5ae944b3fb4c880f825050e0317067b8e9.tar.bz2
perlweeklychallenge-club-51839d5ae944b3fb4c880f825050e0317067b8e9.zip
Challenge 017 task 2
-rwxr-xr-xchallenge-017/jo-37/perl/ch-2.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-017/jo-37/perl/ch-2.pl b/challenge-017/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..82ddec6866
--- /dev/null
+++ b/challenge-017/jo-37/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+use v5.16;
+use warnings;
+
+### Input and Output
+
+main: {
+ my %urlparts = parse_url(shift);
+ say "$_: $urlparts{$_}" for keys %urlparts;
+}
+
+
+### Implementation
+
+# Do not verify the individual parts' syntax.
+
+sub parse_url {
+ shift =~ m{^
+ (?<schema>.+?): # schema is mandatory
+ (?://
+ (?:
+ (?<userinfo>.+?)\@ # userinfo is optional
+ )?
+ (?<host>[^/:]+?) # host is mandatory in authority
+ (?:
+ :(?<port>[^/]+?) # port is optional
+ )?
+ (?=/|$) # path must start with / if authority exists
+ )?
+ (?<path>[^?]*?) # path is mandatory, but may be empty
+ (?:
+ \?(?<query>[^#]+?) # query is optional
+ )?
+ (?:
+ \#(?<fragment>.*) # fragment is optional
+ )?
+ $}x;
+ (%+);
+}