2. App-level ‘ping’ 

# 'Monkey patch' Socket.pm so that when LWP::UserAgent resolves the
# hostname for the HTTPS connection we can override the IP address
# returned (if an address was specified the command-line).

use Socket;

BEGIN {
  no warnings 'redefine';

  my $orig_getaddrinfo = \&Socket::getaddrinfo;
  *Socket::getaddrinfo = sub {
    my($error, @result) = $orig_getaddrinfo->(@_);
    if(my $fake_ip = $opt->{ip_address}) {
      $fake_ip = Socket::inet_aton($fake_ip);
      foreach my $info (@result) {
        my($port, $ip_address) = Socket::unpack_sockaddr_in($info->{addr});
        $info->{addr} = Socket::pack_sockaddr_in($port, $fake_ip);
      }
    }
    return ($error, @result);
  };
}