in reply to Getting "uninitialized value" that I can't figure out...

You could add checks for $ip and $duration in your if($ret) condition. Or you could do sometheting like:
$ip = "No IP found" unless defined $ip; $duration = "No Duration found" unless defined $duration;
That will let you see exactly in which circumstances you are getting undefined values and which values are undefined. Generally it's a good idea to check for definedness of values and either skip trying to print them or else substitute a default value.

Replies are listed 'Best First'.
Re^2: Getting "uninitialized value" that I can't figure out...
by dguntner (Novice) on Jul 22, 2007 at 06:05 UTC
    Thanks, ikegami & jZed. The one I was trying at the time did have a hostname, so it wasn't that which was causing my immediate problem. However, once I applied the fix suggested by syphilis, I was no longer getting the undefined message. Then I did a range, and sure enough, as soon as I hit an address that didn't have a name, the undefined message came back. I applied a version of your '$ip = "no name" unless' fix, and now that part is working just great!

    --Dave

Re^2: Getting "uninitialized value" that I can't figure out...
by apl (Monsignor) on Jul 22, 2007 at 12:46 UTC
    The added benefit is that the question then changes from Why does this printf produce an undef warning? to Why does gethostbyip($host) return undef?.

    print is your friend...
      Are you saying that using print instead of printf there would have given a better idea as to what was causing the error? Yea, the problem is resolved now, but your statement has me curious. Always nice to know if something like that will help me in the future... :-)

      --Dave

        No, sorry for my ambiguity. Let me be pedantic... The code having a problem was:
        printf("%s [ip: $ip] is alive  %.2f ms)\n", gethostbyip($host), 1000 * $duration);

        Using three seperate printf statements would have shown that $duration and $host were defined, and that gethostbyip($host) returned an undef.

        At that point, you could have asked the question Under what circumstances does gethostbyip return an undef?. Your sample code would have also been only four lines long.

        I frequently use lots of printfs (or the debugger) to narrow down a question like this.