#!/usr/bin/perl -- use strict; use warnings; use LWP 5.826; use LWP::Protocol::http; #perldoc -q "How do I find out my hostname/domainname/IP address?" use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); my $ua = LWP::UserAgent->new(); { print "# works like default\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => $addr ); print $ua->get("http://www.example.com")->status_line,"\n"; } { print "# bad hostname\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => "nohost:666" ); print $ua->get("http://www.example.com")->status_line,"\n"; } { print "# unknown error\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => "localhost" ); print $ua->get("http://www.example.com")->status_line,"\n"; printf "$^E\n"; } __END__ # works like default 200 OK # bad hostname 500 Can't connect to www.example.com:80 (Bad hostname 'nohost:666') # unknown error 500 Can't connect to www.example.com:80 (connect: Unknown error) A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond