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

Hi,
I need to get the Domain name of an given URL.
The URLs are dynamic.
For Example:
1. http://shopping.rediff.com/ --> Here domain name is Rediff
2. http://sportsillustrated.cnn.com/?cnn=yes --> Here CNN
3. http://edition.cnn.com/ --> Here CNN
4. http://en.wikipedia.org/ --> Here Wikipedia
5. http://search.cpan.org/ --> Here CPAN

I need a Generalized method to get the domain name.
is there any module to get domain name ?

Replies are listed 'Best First'.
Re: Get Domain name From URLs
by dHarry (Abbot) on Jan 19, 2009 at 14:23 UTC
Re: Get Domain name From URLs
by JavaFan (Canon) on Jan 19, 2009 at 14:43 UTC
    So, for the url 'http://news.bbc.co.uk/', the domain name is 'co'?

    Interesting.

      .oO( Newfound conspiracy theory )

      And you didn't even know bears could type.

Re: Get Domain name From URLs
by Your Mother (Archbishop) on Jan 19, 2009 at 18:06 UTC
    while ( my $raw = <DATA> ) { next unless $raw; my $uri = URI->new($raw); print $uri->host, $/; my ( $bad_assumption_helper ) = $uri->host =~ /([^.]+)\.(?:\w\w\w|\w\w\.\w\w)\z/; print "\t", $bad_assumption_helper, "\n"; } __DATA__ http://shopping.rediff.com/ http://sportsillustrated.cnn.com/?cnn=yes http://edition.cnn.com/ http://en.wikipedia.org/ http://search.cpan.org/ http://news.bbc.co.uk/

    You should heed the snark of the JavaFan though. This isn't a perfect solution and the domain name includes the TLD. A bomb-proof approach to stripping it would require quite a bit more code and probably some lookups. Anything wherein you are required to strip it probably has broken assumptions.

Re: Get Domain name From URLs
by setebos (Beadle) on Jan 19, 2009 at 16:13 UTC
    Since it's a task and not a try to enrich the personal knowledge, just ask google.