SPUG: Scope question

dancerboy dancerboy at strangelight.com
Wed Jun 12 04:30:32 CDT 2002

At 11:54 pm -0700 2002-06-11, Colin Meyer wrote:
>However, there are perfecty valid uses for global variables. In a module
>that is used for manipulating data in a relational database, how tired
>do you get of passing around the database handle into each routine, or
>accessing it from buried within your object. Just put it in a global,
>document that to be your API, and use it when needed.  Another fine use
>of a global is to hold application configuration data.

That might be reasonable advice for non-OO languages.  I still say 
that with OO languages, there is *no* good reason for using globals. 
Pass the database handle to an object constructor once, and then the 
client can forget about it after that -- or else get the handle from 
a class method.  Ditto with configuration data.

The problem with using globals is that they really only work for 
*static* data, data that isn't going to change during the execution 
of a script.  But you can never be certain what data is going to 
remain static, and what data might, in some future version, need to 
be generated dynamically.  Yes, it might seem silly to access a 
configuration value foo as
when all foo does is return a hard-coded value.  It might seem to 
make much more sense to access that hard-coded value as
But you never know when the spec might change such that that 
hard-coded value will need to be replaced by one generated 
on-the-fly.  And when it does, having all your code using accessor 
functions rather than globals will save you a *lot* of headaches.


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list