$wait = 2; # wait up to this many seconds for data # $wait = 5; $start = time(); # zero our vars and then enter the do until loop $timeout = $wait + time(); $data = ''; $count = 0; do { $count++; $data = read_data(); # sleep 1 unless $data; } until $data or time() > $timeout; if ($data) { print "Tried $count times\nGot data '$data'\n"; } else { print "Tried to read_data() but failed after $wait seconds and $count tries!\n"; } # a short sub that only returns data after 3 seconds from # program start time sub read_data { return (time() > ( $start + 3) ) ? "You have waited long enough!" : ''; }