gethostbyname() example
authorGabor Szabo <szabgab@gmail.com>
Wed, 18 Jul 2007 08:47:40 +0000 (11:47 +0300)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Wed, 18 Jul 2007 08:17:47 +0000 (08:17 +0000)
From: "Gabor Szabo" <szabgab@gmail.com>
Message-ID: <d8a74af10707172247v2f761bebs30d054eddbe5861@mail.gmail.com>

p4raw-id: //depot/perl@31623

pod/perlfunc.pod

index c96596f..1afa901 100644 (file)
@@ -2230,6 +2230,18 @@ The Socket library makes this slightly easier:
     # or going the other way
     $straddr = inet_ntoa($iaddr);
 
+In the opposite way, to resolve a hostname to the IP address
+you can write this:
+
+    use Socket;
+    $packed_ip = gethostbyname("www.perl.org");
+    if (defined $packed_ip) {
+        $ip_address = inet_ntoa($packed_ip);
+    }
+
+Make sure <gethostbyname()> is called in SCALAR context and that
+its return value is checked for definedness.
+
 If you get tired of remembering which element of the return list
 contains which return value, by-name interfaces are provided
 in standard modules: C<File::stat>, C<Net::hostent>, C<Net::netent>,