I have used global variables in one form or another for years, and I have been constantly admonished that it is a bad thing to do.
Could someone please provide a cogent argument why?
Surprised I don't have a list of references on this topic yet. Here's a start. Other cool references welcome.
References
From On Coding Standards and Code Reviews:
- Information hiding: Minimize the exposure of implementation details; provide stable interfaces to protect the remainder of the program from the details of the implementation (which are likely to change). Don't just provide full access to the data used in the implementation. Minimize the use of global data. Avoid Action at a distance.
From On Interfaces and APIs:
- Before lexical file handles were introduced in Perl 5.6, those evil old global file handles spawned a host of unfortunate idioms, such as: select((select($fh), $|=1)[0]). In terms of a clear and simple interface to perform a simple task (set autoflush on a file handle), it doesn't get much worse than that, a compelling illustration of why keeping state in global variables is just plain evil. Thankfully, nowadays you can replace it with: $fh->autoflush().