in reply to Re^3: Module to get domain name from hostname
in thread Module to get domain name from hostname

root@sugzum:~/host# cat b2.pl #!/usr/bin/perl use feature qw(say); use Mozilla::PublicSuffix qw(public_suffix); open F, &quot;<host.txt&quot; or die $!; while (<F>) { $hostname = &quot;$_&quot;; } close F; my $tld = public_suffix($hostname) or die &quot;Invalid TLD found in '$hostname'&quot;; $hostname =~ /([^.]+).\Q$tld\E$/ or die &quot;Couldn't find domain name in '$hostname'&quot;; my $domainname= $1; print &quot;Domain name is $domainname.$tld\n&quot;; root@sugzum:~/ssh2smtp# cat host.txt 200-206-30-98.customer.tdatabrasil.net.br root@sugzum:~/ssh2smtp# perl b2.pl Invalid TLD found in '200-206-30-98.customer.tdatabrasil.net.br ' at b2.pl line 9.

Sorry, I'm a beginner. I can't figure it out to understand how this works.

Replies are listed 'Best First'.
Re^5: Module to get domain name from hostname
by Corion (Patriarch) on Jan 30, 2015 at 07:52 UTC

    Your string $hostname still has the newline at the end of it, which is why the error message shows on two lines:

    Invalid TLD found in '200-206-30-98.customer.tdatabrasil.net.br ' at b2.pl line 9.

    Remove all whitespace from the end of $hostname after you've read it from the file:

    $hostname =~ s/\s+$//;

    That way, the call to public_suffix() should succeed.

      Thanks for the help. It works as it should.