in reply to How to handle Net::Address::IP::Local'->public when we are not connected to internet

The eval strategy should work, if the package is indeed throwing an exception (which eval will catch).   But, notice that in the example code above, the value of $@ was printed.   So, the message would appear to “still show up,” even though this time it’s coming from that print statement.

Add two different print statements, one to if part and a different one to the else part, so that you can clearly see which one was executed.

It is proper behavior for the package to have thrown an exception (i.e. die()) in a case where, due to not being connected to the Internet (and therefore having no “local IP-address” at all), no value could sensibly be returned.   You just have to catch that exception, and eval should do that.

Replies are listed 'Best First'.
Re^2: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by Anonymous Monk on Jan 19, 2015 at 17:50 UTC
    Hii, Thamks for the answer. I get this error from that code :
    Unable to create UDP socket: A socket operation was attempted to an un +reachable network....
    And don't this static IP return handle this error ? I mean doing this :
    return $address='192.80.160.16';
    should return return '192.80.160.16' instead of this error message. so how to get rid of this error and return '192.80.160.16' when there is no IP recived (when disconnected) otherwise return the real IP which it receives.
      Just don't print $@ if you don't want this message to be printed.

      Je suis Charlie.
      Instead of using eval string, why don't you simply type it without all the extra quotes?