Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Are global variables "bad"?

by Argel (Prior)
on Apr 21, 2009 at 21:25 UTC ( [id://759122]=note: print w/replies, xml ) Need Help??


in reply to Are global variables "bad"?

I really don't think using globals will come back to haunt you in "mysterious and underhanded ways". You can safely use them, even in large projects. It's just that often in large projects they are more cumbersome and problematic to deal with.

In your case it really comes down to what you think this program will look like a few years from now. If you think it will be about the same size, etc. then I would continue with what you have. If you think there is a good chance it will turn into a much larger and much more complex program then avoiding globals would likely be a better choice. Also, if you do not have a lot of OO experience then you should consider how that will delay your project and how likely you will introduce different types of issues due to that lack of experience.

I guess what I am trying to say is that you should avoid using globals for sound technical reasons instead of fear and cargo cult culture.

Note: I assume by "global" you mean variables declared in the main:: scope (as opposed to e.g. "our" variables).

Elda Taluta; Sarks Sark; Ark Arks

Replies are listed 'Best First'.
Re^2: Are global variables "bad"?
by jpearl (Scribe) on Apr 21, 2009 at 21:47 UTC
    Hmm. Ok, well this brings up another question. I guess I kind of assumed that "our" variables were just another way of declaring variables that could be used throughout the main program. Now that I think of it, I guess that makes no sense. Which, er might mean I'm using that incorrectly.

    If you declare a "local" variable with "my" in the main scope, you can still access it in all of the subroutines in the program. So, how is using "our" different than that? Is that similar to declaring something "public" in an object in java? i.e. if I were creating an object everyone could access (change?) an "our" declared variable, while a "my" declared variable ... not accessible? Or not changeable?

    Update: ok, little trip to our has revealed that I appear to have been completely wrong, except for the parts I was right about. Something which I'm still not clear about though is the accessibility of "our" vs. "my" declared variables. If one can "access" them does that imply the ability to change them? I'm thinking about this as an "our" variable being able to be accessed by something outside the main scope... is it just visible, or is it mutable? (are "my" declared variables visible outside of the scope they are called in? I'm assuming definitely not mutable).
      I think you might like the my() and our() thread. Lots of good links and discussion.

      Elda Taluta; Sarks Sark; Ark Arks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found