gatkins has asked for the wisdom of the Perl Monks concerning the following question:

Hello, dear monks,
 
I'm searching for a module which can find a domain name starting from a hostname. Example:
 
sub-190-88-236ip52.rev.onenet.an >> onenet.an
mailserver2.bite.ac.uk >> bite.ac.uk
bzq135-block4--9170.isdn.net.il >> isdn.net.il
whatever.site.com >> site.com
 
It would be easy if the TLD was just .com / .net / .org. This can be done just by cutting the hostname. The problem is when the TLD is .co.uk or .net.il or .net.br for example.
If you know any module which can do this, please share.
  • Comment on Module to get domain name from hostname

Replies are listed 'Best First'.
Re: Module to get domain name from hostname
by Corion (Patriarch) on Jan 29, 2015 at 15:49 UTC
      Thanks for the prompt answer but this is not what I'm looking for.
      This outputs just the TLD which is net.br for example. I need something that outputs something.net.br (domain, insteat just the TLD)

        If you have stripped away the TLD, the last non-dot characters are the domain name. Most likely, a regular expression is enough:

        my $tld = public_suffix($hostname) or die "Invalid TLD found in '$hostname'"; $hostname =~ /([^.]+).\Q$tld\E$/ or die "Couldn't find domain name in '$hostname'"; my $domainname= $1; print "Domain name is $domainname.$tld\n";