Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Why 'Net::Address::IP::Local'->public do not return IP address

by ppp (Acolyte)
on Jan 16, 2015 at 18:10 UTC ( [id://1113534]=perlquestion: print w/replies, xml ) Need Help??

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

I am perl beginner and under a situation where i have a dongle (USB internet key). When USB is connected and i do :

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

then it works fine and return an IP address. But i have situation where i have to check disconnect the USB and then check it's IP address using same code but on disconnection it gives an error on writing this command and it is:

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.

Yes ofcourse it will give error because it will not return any IP address when disconnected. But i wann handle this error. And assign a static IP when it is disconnected like "192.80.13.1". I tried to handle this error this way but out of success:

sub ReturnIpAddress { if( 'Net::Address::IP::Local'->public =~ m/^(\d\d?\d?)\.(\d\d?\d?)\. +(\d\d?\d?)\.(\d\d?\d?)$/ ) { return $address= 'Net::Address::IP::Local'->public; #When i try to + print this address it print nothing s it crashes because of the erro +r due to the enteres command i discussed before. } else { return $address='192.80.160.16'; } }

How to handle this error and assign statically $address='192.80.160.16' when USB key is disconnected ?

Replies are listed 'Best First'.
Re: Why 'Net::Address::IP::Local'->public do not return IP address
by Laurent_R (Canon) on Jan 16, 2015 at 18:31 UTC
    Try to put your block of code in an eval function (and make sure to check the content of the $@ special variable).
    Je suis Charlie.
      you mean something like this : eval { $address='Net::Address::IP::Local'->public =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/ }; warn $@ if $@;
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1113534]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found