You can use
Sys::HostIP (thanks for the tip
ybiC which seems to work fine. If you don't want to use a module for one simple function, here's a light weight alternative that works out great for me.
Here's a program that demonstrates it in action.
$addr = $^O eq 'MSWin32' ? slurp_ip('ipconfig') : slurp_ip('/sbin/ifco
+nfig eth0');
sub slurp_ip {
my $cmd = shift;
my ($ip, $name);
$name = hostname();
$ip = join(".",unpack("C4",(gethostbyname($name))[4]));
if (!$ip) {
$ip = $1 if `$cmd` =~ /(\d+\.\d+\.\d+\.\d+)/;
}
return $ip;
}