That is a big, BIG chunk of code. Why do you want to put it into an eval? Why not just change it into a subroutine, and call it?
If you're changing it dynamicaly in your eval, are you sure that what is getting executed is actualy what you expect?
Have you tried stepping through the code in a debugger while it executes? Did you look what is happening step by step and
check the results of each statement to see if it's getting what you expect? Because I bet the first problem happens way before that print statement.