in reply to What does the error 'Illegal division by zero' mean?

Well, it looks like when the code in $code is evaluated, the length method returns 0, so $n is never incremented, so it's undef when the END block is run. For the purpose of the division, that undef is converted to 0, and there you are---division by 0.

The easiest way to fix this is to check if $n is 0, and if so don't divide by it. The hard part is figuring out what you want to do instead. :)