tags:

views:

645

answers:

4

How do I extract the domain out of an URL? Is there a Perl Module? I could not find any.

A: 

I would just use a Regular Expression, although you could also do it with a series of substring operations..

Miky Dinescu
No point re-inventing the wheel.
David Precious
@David, you're right..
Miky Dinescu
+1  A: 

Perhaps Regexp::Common:URI?

Update: URI as suggested by friedo sounds better?

toolkit
+14  A: 

The URI module can parse URIs for you in a nice OO-ish way. To get the domain part:

my $url = URI->new( "http://www.stackoverflow.com/" );
my $domain = $url->host;
print $domain;
friedo
Thanks, this is exactly what I was looking for.
Timmy
The link in the answer points to this very question. I think you mean it to be: http://search.cpan.org/~gaas/URI-1.53/URI.pm
Dennis Williamson
A: 

Using the regex method does have some pitfalls, for one he can test the url with the other methods, like the type (http, ftp, https, gopher, xmmp) and it will handle a wide variety of hosts (name, ipv4, ipv6), and handle the file: and unc ape-crazy stuff.

hpavc