in reply to Re^2: Why 'Net::Address::IP::Local'->public do not return IP address
in thread Why 'Net::Address::IP::Local'->public do not return IP address

Well, yes, something like that, or possibly including a bit more of your code in the eval block. You should also probably check the value returned by eval, it might be useful.

I can't test right now anything really similar to your code, but this "several-liner" should give you an idea of how the eval function works:

perl -E ' use strict; use warnings; my $output; say "successful statement"; $output = eval {my $c = 4 + 5;}; printout ($output, $@); say "division by 0"; $output = eval {my $c = 4/0;}; printout ($output, $@); say "statement with die"; $output = eval {die "I am dieing\n";}; printout ($output, $@); sub printout { my ($out, $diag) = @_; say $out, "\t", $diag, "\n" if defined $out; say "\t\t$diag" unless defined $out; } ' successful statement 9 division by 0 Illegal division by zero at -e line 12. statement with die I am dieing
Je suis Charlie.

Replies are listed 'Best First'.
Re^4: Why 'Net::Address::IP::Local'->public do not return IP address
by Anonymous Monk on Jan 19, 2015 at 17:26 UTC
    Bonjour, I tried something like this :
    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; } }
    And i got error again like this :
    C:\shekhar_Axestrack_Intern\WindowCreation>ccc.pl Unable to create UDP socket: A socket operation was attempted to an un +reachable network. at C:/Perl/site/lib/Net/Address/IP/Local.pm line 166. 192.80.160.16
    Any solution. Please note that this problem occurs when i disconects the internet and then execute the code (i am under such situation). Any other way to handle this problem ? (To test the code you will need to resatrt the PC and with internet turnoff otherwise it still keeps store the IP of the time of connection and print the IP but when we restart PC then we will not have that previous IP)
      But that was what you wanted, wasn't it?

      When you do this:

      print "$@";
      you ask your program to print the error during the creation of the socket, and that's what you get:
      Unable to create UDP socket: A socket operation was attempted to an un +reachable network. at C:/Perl/site/lib/Net/Address/IP/Local.pm line 166.
      If you don't want to get this warning printed, then just don't print it.

      But now, your program does not die anymore because of this network exception, this is no longer a fatal error, since your subroutine is able to continue with the next line of code and to return the 192.80.160.16 IP address.

      Or did I misunderstand what you were after?

      Je suis Charlie.