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,
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||