Take the 2-minute tour ×

Consider the following snippet of a Perl script:

use IO::Socket;
# ...
    PeerAddr => $host, # e.g. "google.com"
    PeerPort => $port, # e.g. 80
    Proto => 'tcp'
die("no socket: $!") unless $sock;
# ...

Everything works as expected under normal conditions but when then host system's internet connection is inactive the "sock" variable is empty and $! has the message "Invalid argument".

Am I using the INET constructor inappropriately or is this the expected behavior? If the latter, is there a way to differentiate between a "network interface inactive" error and a genuine invalid argument to the constructor method?

share|improve this question
Try $^E instead of $! –  ikegami May 23 '11 at 18:35
@ikegami: unfortunately, neither Perl 5.8.9 on Mac OS 10.6.7 nor Perl 5.10.1 on Linux 2.6.35 (Ubuntu 10.10) have $^E different from $! for this error. –  maerics May 23 '11 at 18:44
Then sorry, that's all the information the system provides. Or maybe there's an error check missing somewhere, and the real error is being overwritten by a useless one. –  ikegami May 23 '11 at 18:48

1 Answer 1

up vote 4 down vote accepted

You are seeing "Invalid argument" because the constructor tries to resolve the hostname, gets an error, and returns EINVAL in $!. If you would use an IP address in $host, you would see the real error, "Network is unreachable".

Also, IO::Socket::INET sets $@ to qualify the error returned in $!, so if you print $@ as well as $!, you will see "Bad hostname 'google.com'", which is probably an even better diagnostic than "Network unreachable" you would get with an IP address instead of the hostname. In both cases it should be immediately clear what is going on.

share|improve this answer

Your Answer


