blib/lib/Net/Domain/Parts.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Net::Domain::Parts;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Exporter;
11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(
13 domain_parts
14 );
15  
16 our @EXPORT_OK = qw(
17 tld_list
18 tld_struct
19 );
20  
21 our %EXPORT_TAGS;
22 $EXPORT_TAGS{all} = [@EXPORT, @EXPORT_OK];
23  
24 our $VERSION = '0.04';
25  
26 my $json;
27 {
28 local $/;
29 $json = <DATA>;
30 }
31  
32 my $tlds = decode_json($json);
33  
34 sub domain_parts {
35 my ($domain_name) = @_;
36  
37 if (! defined $domain_name) {
38 croak(__PACKAGE__ . "::domain_parts() must be sent in a domain name.");
39 }
40  
41 if ($domain_name !~ /.+\./) {
42 croak(__PACKAGE__ . "::domain_parts() domain param invalid: $domain_name");
43 }
44  
45 my ($sub_domain, $domain, $tld) = _sort_domain_parts($domain_name);
46  
47 return($sub_domain, $domain, $tld);
48 }
49 sub tld_list {
50 my %tld_list;
51  
52 for my $key (keys %$tlds) {
53 next if $key =~ /version/;
54 $tld_list{$_} = 1 for keys %{ $tlds->{$key} };
55 }
56  
57 return \%tld_list;
58 }
59 sub tld_struct {
60 return $tlds;
61 }
62 sub version {
63 return $tlds->{version};
64 }
65  
66 sub _sort_domain_parts {
67 my ($dns_entry) = @_;
68  
69 my $dot_count = () = $dns_entry =~ /\./g;
70  
71 my $tld_match;
72  
73 if ($dot_count > 2) {
74 # Could possibly be a three-level TLD
75  
76 my $tld_entries = $tlds->{third_level_domain};
77  
78 for (sort { length($b) <=> length($a) } keys %{ $tld_entries }) {
79 if ($dns_entry =~ /$_$/) {
80 $tld_match = $_;
81 last;
82 }
83 }
84 }
85 if (! $tld_match && $dot_count > 1) {
86 # Could possibly match a second-level TLD (eg. co.uk)
87  
88 my $tld_entries = $tlds->{second_level_domain};
89  
90 for (sort { length($b) <=> length($a) } keys %{ $tld_entries }) {
91 if ($dns_entry =~ /$_$/) {
92 $tld_match = $_;
93 last;
94 }
95 }
96 }
97 if (! $tld_match) {
98 # If no match yet, check the domain against top-level TLDs
99  
100 my $tld_entries = $tlds->{top_level_domain};
101  
102 for (sort { length($b) <=> length($a) } keys %{ $tld_entries }) {
103 if ($dns_entry =~ /$_$/) {
104 $tld_match = $_;
105 last;
106 }
107 }
108 }
109  
110 return(undef, undef, undef) if ! $tld_match;
111  
112 my ($sub_domains, $domain);
113  
114 $dns_entry =~ s/\.$tld_match//;
115  
116 if ($dns_entry =~ /(.*)\.(.*)/) {
117 $sub_domains = $1;
118 $domain = "$2.$tld_match";
119 }
120 else {
121 $domain = "$dns_entry.$tld_match";
122 }
123  
124 return($sub_domains, $domain, $tld_match)
125 }
126  
127 sub __placeholder {}
128  
129 1;
130  
131 =pod
132  
133 =head1 NAME
134  
135 Net::Domain::Parts - Extract sub-domain, domain and TLD parts of a domain name.
136  
137 =for html
138 <a href="https://github.com/stevieb9/net-domain-parts/actions"><img src="https://github.com/stevieb9/net-domain-parts/workflows/CI/badge.svg"/></a>
139 <a href='https://coveralls.io/github/stevieb9/net-domain-parts?branch=main'><img src='https://coveralls.io/repos/stevieb9/net-domain-parts/badge.svg?branch=main&service=github' alt='Coverage Status' /></a>
140  
141  
142 =head1 SYNOPSIS
143  
144 use Net::Domain::Parts;
145  
146 my $domain_name = 'www.perlmonks.org';
147  
148 my ($subdomain, $domain, $tld) = domain_parts($domain_name);
149  
150 printf(
151 "Domain %s, Subdomain %s, TLD: %s\n",
152 $domain,
153 $subdomain,
154 $tld
155 );
156  
157 # Domain: perlmonks.org, Subdomain: www, TLD: org
158  
159 =head1 DESCRIPTION
160  
161 This module takes a fully qualified domain name, and breaks it down into its
162 core components... the TLD, the domain name itself, and any subdomains.
163  
164 =head1 FUNCTIONS
165  
166 =head2 domain_parts($domain_name)
167  
168 Exported by default. Breaks up an FQDN into it's core components.
169  
170 I<Parameters>:
171  
172 $domain_name
173  
174 I<Mandatory, String>: A fully qualified, valid domain name string.
175  
176 I<Returns>: A list of C<subdomain> (undef if not present), C<domain> (with TLD
177 attached) and the C<tld>. If the domain isn't valid (ie. doesn't exist), three
178 C<undef>s will be returned.
179  
180 =head2 version
181  
182 Never exported. Call it as a fully qualified package function. Returns the
183 version string of the TLD data being used.
184  
185 my $version = Net::Domain::Parts::version();
186 print "$version\n";
187  
188 # 2025-01-21_09-07-06_UTC
189  
190 =head2 tld_struct
191  
192 Not exported by default. Returns the internal store of TLD data.
193  
194 I<Returns>: Hash reference.
195  
196 {
197 version => 'version_string',
198 third_level_domain => {
199 'witd.gov.pl' => 1,
200 'fudai.iwate.jp' => 1,
201 },
202 second_level_domain => {
203 'co.uk' => 1,
204 'prato.it' => 1,
205 },
206 top_level_domain => {
207 'com' => 1,
208 'ca' => 1,
209 'org' => 1,
210 },
211 }
212  
213 =head2 tld_list
214  
215 Not exported by default. Returns the entire list of TLDs, each TLD as a hash
216 key.
217  
218 I<Returns>: Hash reference.
219  
220 {
221 'org' => 1,
222 'com' => 1,
223 'co.uk' => 1,
224 'org.im' => 1,
225 'kashiwazaki.niigata.jp' => 1,
226 }
227  
228 =head1 AUTHOR
229  
230 Steve Bertrand, C<< <steveb at cpan.org> >>
231  
232 =head1 LICENSE AND COPYRIGHT
233  
234 Copyright 2025 Steve Bertrand.
235  
236 This program is free software; you can redistribute it and/or modify it
237 under the terms of the the Artistic License (2.0). You may obtain a
238 copy of the full license at:
239  
240 L<http://www.perlfoundation.org/artistic_license_2_0>
241  
242 =cut
243  
244 __DATA__
↑ Summary