Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Net::Ping duration question

by Anonymous Monk
on Jun 21, 2012 at 05:13 UTC ( #977557=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


I will make this quick. I'm following the example right out of the description page.
while ( 1 ) { my $ret; my $duration; my $ip; ( $ret, $duration, $ip ) = $p->ping( $host, 5.5 ); printf( "$host [ip: $ip] is alive (packet return time: %.2f ms +)\n", 1000 * $duration ); }
The result I get back for $duration * 1000 is either 0 or 1000 ms. The problem is, the host I'm using has an average ping time of 127ms, so something just isn't adding up. Am I missing something?

Replies are listed 'Best First'.
Re: Net::Ping duration question
by frozenwithjoy (Priest) on Jun 21, 2012 at 05:29 UTC
    Which version of perl do you have? More specifically, is Time::HiRes part of your core? It should be unless you are on a Commodore64 or something else from my childhood...

    If so, this should fix your problem: $p->hires();. Put it right after $p = Net::Ping->new();

      That did it, thanks. I got no error so assumed Time:HiRes was there, but didn't see I needed to make that call. All bueno now!
        Perfect! By the way, if you print out $ret, what do you get? I keep getting 0, even though I'm getting realistic duration values. I was under the impression that I'd get $ret == 1 if the host was reachable.
Re: Net::Ping duration question
by NetWallah (Canon) on Jun 21, 2012 at 05:34 UTC
    Try setting
    before the ping call. This will cause Net::Ping to use Time::HiRes, and return a Float, instead of using normal (1 second) resolution, and returning an Integer.

    Of course, this requires Time::HiRes to be installed/available.

    Update: Dang - frozenwithjoy (++) has a higher Res, and beat me to the punch.

                 I hope life isn't a big joke, because I don't get it.

      Come on up here with me. There's plenty of glory to go around and no one I'd rather share it with more than you!

      edit: Oh noes! down-voted! I guess I better stop enjoying myself while I'm spending my time helping people out. ^___^

        Its past my bedtime now, so I'll let you bask!. Thanks for the invite, and Cheers!
        We'll play again on the morrow.

                     I hope life isn't a big joke, because I don't get it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977557]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-05-21 00:11 GMT
Find Nodes?
    Voting Booth?