String/Hostname.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« String/Hexa.pm ↑ Summary String/Html.pm »
1 #
2 # $Id$
3 #
4 # string::hostname Brik
5 #
6 package Metabrik::String::Hostname;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable fqdn domain) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 hostname => [ qw(hostname) ],
20 },
21 commands => {
22 parse => [ qw(hostname|OPTIONAL) ],
23 },
24 };
25 }
26  
27 sub parse {
28 my $self = shift;
29 my ($hostname) = @_;
30  
31 $hostname ||= $self->hostname;
32 $self->brik_help_run_undef_arg('parse', $hostname) or return;
33  
34 my $tld = '';
35 my $domain = '';
36 my $host = '';
37 my @subdomain_list = ();
38 my @toks = split('\.', $hostname);
39 if (@toks == 1) {
40 $host = $toks[0];
41 }
42 elsif (@toks == 2) {
43 $tld = $toks[1];
44 $domain = $toks[0].'.'.$tld;
45 }
46 elsif (@toks == 3) {
47 $tld = $toks[2];
48 $domain = $toks[1].'.'.$tld;
49 $host = $toks[0];
50 }
51 elsif (@toks > 3) {
52 $tld = $toks[-1];
53 $domain = $toks[-2].'.'.$tld;
54 $host = $toks[0];
55 my $count = @toks - 3;
56 my $last = $domain;
57 for my $t (reverse 1..$count) {
58 $last = $toks[$t].'.'.$last;
59 push @subdomain_list, $last;
60 }
61 }
62  
63 return {
64 host => $host,
65 domain => $domain,
66 subdomain_list => \@subdomain_list,
67 tld => $tld,
68 };
69 }
70  
71 1;
72  
73 __END__
« String/Hexa.pm ↑ Summary String/Html.pm »