use Net::DNS::DomainName; my $host = "foo.example.com"; my $bare = (Net::DNS::DomainName->new($host)->label)[0]; print "$bare\n"; # prints "foo"