in reply to General style advice requested

$cycle_length being declared globally is not acceptable. I would declare $first and $second in the loop, but they're acceptable where they are.

The other issue it's very hard to spot the loop in your sub. May I recommend

for (;;) { # I read this as "for ever" ... }

or the more popular

while (1) { ... }