http://www.perlmonks.org?node_id=100886


in reply to NEVER do this. Ever. Not even once.
in thread Variable Scope

Care to explain why?
For quite a long time (before we started Object-Orienting the project) we used global (::) variables to hold our internal database. Though I can understand why under certain circumstances it might not be wise, to that end we found it appropriate.(And there were mounds of vars there).
In fact, I have often found myself looking around in other people's code, trying to figure out which of the hundreds of hashes, arrays and scalar defined at the beginning of the script (outside the scope) they were referring to, and where they were changed. At least when they were ::'ed I knew I had to go chase them...
I appreciate the problems this can cause across packages, but sometimes your scripts standalone.

If I am missing something - please tell me,
--- perchance
  • Comment on Re: NEVER do this. Ever. Not even once.

Replies are listed 'Best First'.
Re: Re: NEVER do this. Ever. Not even once.
by chromatic (Archbishop) on Jul 31, 2001 at 04:02 UTC
    I understood dragonchild to mean that using $::somevar is an awful practice. I quite agree. What's the point of telling Perl, "I want you to alert me to potentially hazardous programming" when you go out of your way to violate it?

    As for global variables, they're hard to track down, they can have unfortunate side effects, they can make your code difficult to follow, they break encapsulation, they reduce reusability, and they can lead to severe contortions when you add new things.

    That's not to say that they're forbidden. bikeNomad points out a place where it's absolutely necessary. It's even useful.

    It's just that, in general, the places where you really need to use global variables are few and far between. Appropriately scoped lexicals exist for a reason.