in reply to Avoiding Globals with OO Perl
I tend to store these global parameters in a hash I call %config and many times I load it from an external config file.
Actually what you are doing is more or less the same, only you have gone one step further and use an object. So you just made it a bit more complex with no benefits to compensate for this complexity. Your way to get data in or out of your object is more difficult than using a hash. It does not protect you from any typos, since you do not even check in your getter method if that slot in your object ever existed beforehand and it is now being created silently (and perhaps wrongly).
I guess your idea is to use this global object to move data in and out of your subroutines without having to use any variable as parameters. I can tell you that is the wrong approach. Subroutines should have a well-defined input and output interface, which should be obvious to any programmer, i.e. the first line of your sub should grab the data in the @_ variable and store it in lexical variables (with meaningful names) and all data returned by the sub should pass through a return statement. A sub should not "leach" or "leak" information in any other way.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Avoiding Globals with OO Perl
by Anonymous Monk on Oct 20, 2011 at 20:02 UTC | |
by BrowserUk (Patriarch) on Oct 20, 2011 at 20:15 UTC | |
by GrandFather (Saint) on Oct 21, 2011 at 11:14 UTC | |
by fisher (Priest) on Oct 24, 2011 at 06:02 UTC |