Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

•Re: Global variable vs passing variable from sub to sub

by merlyn (Sage)
on Sep 13, 2004 at 16:24 UTC ( #390594=note: print w/ replies, xml ) Need Help??


in reply to Global variable vs passing variable from sub to sub

Both of them are bad. Both "passing around" and "global variables" are a signs of a misdesign.

If you have a value that must be shared, make it a "regional" variable, accessible to a few subroutines that deal with it, and in turn provide a higher-level interface that don't need to expose that variable. For example, if you're talking about something like a database handle, create a module that does all the DB stuff, and then you can initialize (perhaps lazily) the database handle there. Thus, the rest of the program does not have access to the database handle, but you still don't have to pass it around a lot.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


Comment on •Re: Global variable vs passing variable from sub to sub
Re^2: Global variable vs passing variable from sub to sub
by kiat (Vicar) on Sep 13, 2004 at 16:35 UTC
    Thanks, merlyn!

    Hm...I'm not quite sure if what I'm currently labelling as "global" is in fact "regional" in my implemenation. The "global" I mentioned in the parent node is used in a module and is shared by a couple of subroutines. If I understand you correctly, that's okay right?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://390594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (18)
As of 2014-08-22 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (161 votes), past polls