in reply to Regex not working?

Your problem is in chooser().   if ($selection !~ /[1-$length]/) { ... isn't doing what you expect. It will accept (or reject, though the effect is to accept) any string that includes at least one digit in the given range.