use strict; use warnings; require IO::Socket::IP; # Implicitly included above, yet list it explicitly: require Socket; # An IPv6 IP would work fine here too. # Feel free to hard-code the CDN IPv6 resolution if you'd like. my $host = "google.com"; my $sock = IO::Socket::IP->new( Proto => Socket->IPPROTO_TCP, Type => Socket->SOCK_STREAM, # This will force a particular address-family. # If you leave it off, the system's gethostbyname() call # will attempt to determine this automatically: Family => Socket->AF_INET6, PeerHost => $host, PeerPort => 80, Timeout => 3, ) or die "Failed to create socket: $!"; # Use Perl/IO (built on Standard I/O) to send a request for data: $sock->print("GET /\n") or die "Socket error on write: $!"; # Read loop, printing out data from socket to STDOUT. my $rc; while (1) { # Attempt to read up to 1k of data: $rc = $sock->read(my $buffer, 1024); # catch socket errors (excluding EOF): unless (defined $rc) { die "Socket error on read: $!"; } # EOF returns 0, which exits the read loop: last if ($rc == 0); # Otherwise print the line, which may be a partial line. printf "%s", $buffer; } print "\n";