You have two servers, A1.com and A2.com, and A2.com is the backup mirror of A1.com. You want to make one HTTP request to A2.com but pretend that the DNS resolves A1.com to its IP. This snippet allows you that, by taking over the creation of new IO::Socket objects.
The correct Host: header for A1.com is still sent, so name-based virtual hosting works as it should, too.
Update: Failover is also a good keyword for this node...
use strict;
use IO::Socket::INET;
use LWP::Simple;
my $url = 'http://www.google.com';
my $backup_host = 'www.apache.org';
getprint $url;
{
my $old_constructor = \&IO::Socket::INET::new;
local *IO::Socket::INET::new = sub {
my ($package,%args) = @_;
warn "Redirecting from $args{PeerAddr} to $backup_host";
$args{PeerAddr} = $backup_host;
$old_constructor->($package,%args);
};
getprint $url;
};