in reply to Re^2: Waiting for input, without sleep
in thread Waiting for input, without sleep

Yes, peek() and pending() are intended as non-blocking calls that you can use to get an idea of what will happen if you issue a read().

If I understand your question correctly, you desire a more efficient way to wait until the response from the server is available. This is typically done by:
while($data = $sock->peek()){ if($data eq $what_you_want){ &DO_YOUR_STUFF; last; }else{ sleep 1; } }

It's not totally necessary to have that short sleep, but typically you want some type of pause so that your script doesn't get into a tight loop and hog the cpu.

If you don't know what data you expect back, you can use pending() instead of peek() to see the amount of data waiting to be read().

Replies are listed 'Best First'.
Re^4: Waiting for input, without sleep
by Trihedralguy (Pilgrim) on Jun 30, 2009 at 16:12 UTC
    Hrm. For some reason Peek keeps giving me "0" and it falls out if the while loop right away:

    while($response = $sock->peek()) { if ($response ne '') { #verify the data print "Server Response: $response\n"; $response = (string2hex($response)); print "Server Response: $response\n"; last; } else { sleep 1; print "Waiting for a response!\n"; } }
      You're using a string operator on a number in your if statement. Try changing
      if($response ne ''){ ... }else{ ... }

      to
      if($response > 0){ ... }else{ ... }

        So the message I get back then is still 0? Should I still be using sysread?