ppp has asked for the wisdom of the Perl Monks concerning the following question:

I am under a situation where i am trying to get the current IP address with time. The code below works fine when there is connection of internet and it return the IP as desired.

my $address = 'Net::Address::IP::Local'->public ;

The problem is my code also has to run if there is No internet connection, so the problem occurs when i am disconnected and still run the same code it breaks my all code because there is no IP written by it as we are disconnected from internet

Now i have though some solution of it but out of luck!!!! The solution is wheni receive no IP address then i should assign a static IP address like this ($address='192.80.160.16';) But it do not work. My try to do it is below :

sub ReturnIpAddress { select STDOUT; my $address = 'Net::Address::IP::Local'->public ; ## Receiving the c +urrent IP ## if(!$address) { return $address='192.80.160.16'; ##If No IP address received then +return a static IP of order "192....." } else { return $address; } }

But it still do not work. When i try to print that IP adress on disconnect then it prints nothing (If it was like Null or 0 in c++ i could have handle it but i dont know how to handle it by doing something like this (if($address==null){ $address='192.80.160.16';}) in case of perl)

Any help please ?

Replies are listed 'Best First'.
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet (INADDR_ANY)
by tye (Sage) on Jan 19, 2015 at 16:11 UTC

    After perusing Net::Address::IP::Local, I think that perhaps you are getting back Socket::INADDR_ANY, which is likely "\0\0\0\0", which is a true value.

    I expect that the next thing that your code does is to call Socket's inet_ntoa(). So you could also provide a default if that returns "0.0.0.0".

    - tye        

      I tried the code below but still it gives error :
      use strict; use warnings; use Net::Address::IP::Local; print ReturnIpAddress(); sub ReturnIpAddress { my $address; select STDOUT; eval{ $address = 'Net::Address::IP::Local'->public ; ## Rece +iving the current IP ## }; if ($@) { print "$@"; return '192.80.160.16'; ##If No IP address received th +en return a static IP of order "192....." } else { return $address; } }
      error is :
      Unable to create UDP socket: A socket operation was attempted to an un +reachab network. at C:/Perl/site/lib/Net/Address/IP/Local.pm line 166.
        In the post you included in your other thread on the same subject, the output you gave showed the socket error and then the printed IP address, so that it appears that this part of your code is working properly, your program non longer dies on the error and is able to print the IP address after the socket open failure.

        Je suis Charlie.
      Yes it gives some UDP socket connection couldnt created.. type of error. I dont have that in pc here. I saw in my school this morning. But error was linke some .. UDP socket cudnt create.. So how to handle it . I t gives this error because there is no internet access and even then i am trying to get IP , so no connection i guess thats why.. But how i can handle it ? Because if i print it like this :
      print "adress is : 'Net::Address::IP::Local'->public";
      Then it just print:
      adress is :
      Now it even do not print 0 or null so that i could habdle it like this if($adress=null) {do this} How to handle ?
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by vinoth.ree (Monsignor) on Jan 19, 2015 at 16:16 UTC

    When i try to print that IP adress on disconnect then it prints nothing

    Did you check Net::Address::IP::Local::Error exception is thrown?


    All is well
      Yes it gives some UDP socket connection couldnt created.. type of error. I dont have that in pc here. I saw in my school this morning. But error was linke some .. UDP socket cudnt create.. So how to handle it . I t gives this error because there is no internet access and even then i am trying to get IP , so no connection i guess thats why..

      But how i can handle it ? Because if i print it like this :

      print "adress is : 'Net::Address::IP::Local'->public";
      Then it just print:
      adress is :
      Now it even do not print 0 or null so that i could habdle it like this if($adress=null) {do this} How to handle ?

        Try the same with eval. Here is the code, I have added eval part

        use strict; use warnings; use Net::Address::IP::Local; print ReturnIpAddress(); sub ReturnIpAddress { select STDOUT; eval{ $address = 'Net::Address::IP::Local'->public ; ## Rece +iving the current IP ## }; if ($@) { print "$@"; return '192.80.160.16'; ##If No IP address received th +en return a static IP of order "192....." } else { return $address; } }

        All is well

        If you’re really doing this, and it’s not just a typo, it’s not what you think. Package and object stuff does not work as interpolated strings. Well, not without trickery. This–

        print "adress is : 'Net::Address::IP::Local'->public";

        –will print this–

        adress is : 'Net::Address::IP::Local'->public
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by locked_user sundialsvc4 (Abbot) on Jan 19, 2015 at 17:38 UTC

    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.

      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?
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by locked_user sundialsvc4 (Abbot) on Jan 20, 2015 at 00:42 UTC

    I (still ...) strongly suspect that you are seeing the error-message because of print $@;.   Therefore, I specifically suggest that you should modify the if/else structure in more or less this way:

    if ($@) { print STDERR "An exception was caught!\n"; # (set the default IP-address here, etc ...) } else { print STDERR "An exception did not occur.\n" # ( grab the real address here) } print STDERR "... so the IP-address is $ipaddr\n"; # (etc. etc ...)
    In other words, cause the code to explicitly log what it’s doing, one way or the other, to remove all doubt as to what it is doing.