in reply to global variables vs global hash
I'm working on a script and currently have a lot of global variables. Is it better to create a couple global hash key/values and incorporate the global vars into the hash?Unlikely. What makes you think that's an improvement? "Don't have (too) many global variables1,2" is a good practice, but it's only useful if you know why. If you know why, you wouldn't consider putting them in a hash - it doesn't solve any of the problems associated with global variables, and introduces more problems.
Note it doesn't mean every global variable is "bad" - there are many cases where a global variable makes sense. And note there are many "hidden" global variables; in a naive, hashref based objects, its attributes are "global" as well - much more code can (unintended) touch them.
In doing so would this also speed up the script or slow it down?Probably slow it down. But it's unlikely you'll notice the difference.
| 1 | It's actually a misnomer. The potential problems have to do with variables having a too broad scope. Even if a variable isn't truely global, it can still have a scope that's too broad. |
| 2 | Some people think "global variable" means "package variable". It doesn't. A lexical variable can be a global variable as well. You don't solve the "don't have global variables" issue by just sticking a my in front of them! |
|
|---|