in reply to Re: Re: Getting IP from URL
in thread Getting IP from URL

Hrmmm, in light of this clarification, I think something like the following may be best ...

use Socket; use URI; my $url = 'http://www.mydomain.com'; my $uri = URI->new( $url ); my $ip_addr = gethostbyname( $uri->host ); $ip_addr = inet_ntoa( $ip_addr ); if ( $uri->host eq $ip_addr ) { #... Creatures evolve, code does stuff }

The advantage that this method of employing the URI module over a simple regular expression is the correct handling of more complex URLs (which may incorporate username and password authentication details in the form of scheme://username:password@host:port/) and validation of IP addresses.

 

perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001001")),"\n"'

Replies are listed 'Best First'.
Re: Re: Re: Re: Getting IP from URL
by PodMaster (Abbot) on Oct 05, 2002 at 10:49 UTC
    There really isn't any need for that socket stuff.
    use URI; my $url = 'http://foo.bar.com'; my $uri = new URI($url); if( $uri->host =~ /[a-zA-Z\-]/ ) { # or /[^\d\.]/ # it's an IP dag-nabbit }

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      Agreed, this approach does simplify things somewhat - The incorporation of the Socket stuff does however have the advantage of ensuring that the IP address (hostname) extracted is valid.

       

      perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001010")),"\n"'