in reply to using eval or equivalent to automatically generate my statements
Hello ISAI student,
To expand a little on tobyink’s answer:
My reaction to your question is: Take a step back, and look at the wider picture. Modularity is an excellent goal, but why? Because a script composed of self-contained modules, loosely coupled together, is easier to debug/maintain/enhance than a monolithic script “riddled with global variables” (to use your words). But conversion of global variables into lexical variables is a means to this end, not a goal in itself. The aim is to produce modularity and loose coupling: global variables shared across a script entail tight coupling.
Now, I could be wrong, but it seems to me that your proposed solution is to retain global variables in all but name — to have the same variables shared across the whole script, but to make these variables lexical via a monster hash which is passed by reference to all the subroutines. The likely result is that you will end up with the same tight coupling as before, but implemented in a more complicated syntax which is even harder to debug/maintain/enhance than the original.
The bottom line is this: If you want the benefits of modularity, you must have loose coupling. And to achieve loose coupling, you must identify which variables need to be shared between modules, and refrain from sharing any which do not. For this, there is, alas!, no easy fix — you have to re-design and re-factor before you can re-code. And the measure of success is this: the end result must be simpler and clearer than the original.
I know this advice is not what you were hoping to hear, but I think it’s good advice all the same. I hope it helps,