in reply to Finding the Top Level Domain from a URL

Your code seemingly intentionally finds something other than the TLD in some situations.

It also mishandles a number of the valid urls listed below. Fix:

$url = URI->new($url); defined( my $host = $url->host() ) or die("No host\n"); my $tld; if ($host =~ /\./) { $tld = /\.([^.]+)$/; $tld =~ /[a-z]/i or die("No domain\n"); } else { $host =~ /[a-z]/i or die("No domain\n"); $tld = 'localdomain'; }

Handles valid urls

Invalid urls aren't necessarily detected.

Update: Updated code to detects an invalid url it didn't detect before.