Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Are global variables "bad"?

by DStaal (Chaplain)
on Apr 21, 2009 at 16:36 UTC ( #759051=note: print w/replies, xml ) Need Help??


in reply to Are global variables "bad"?

Your concerns about having them come back and bite you are probably warranted, in my experience. While it is certainly possible to write and maintain good code that uses global variables, it takes a level of dedication to self-discipline, and a good strategy to keep them separate from any other variables you may be using at any particular point in time.

Since you can create 'setter' and 'getter' functions for them, how about putting them in an object? Then you can pass the object around, and it will take less dedication to preserve the encapsulation around the variables.

There are several ways to do objects in Perl, depending on how much you want to do and how complex you want the result. I've heard good things about Moose, although I've yet to use it. (I normally work directly with inside-out objects, to reduce the number of dependencies.)

Replies are listed 'Best First'.
Re^2: Are global variables "bad"?
by jpearl (Scribe) on Apr 21, 2009 at 17:23 UTC
    Thanks DStaal,to be honest, coming from a java background, I definitely am drawn towards making this object oriented. Actually creating the getter and setter functions were definitely an attempt to emulate OO style programming in my (currently) almost completely functional style program. I certainly am eying perl OO surreptitiously, but then I'd have to figure out how bless works. Though, I suppose that's probably going to have to happen soon anyway.

      Quick version on bless: It marks a reference as an object. The reference can be to any type of varible: scalar, array, hash, are all common. You will need to figure out some way of accessing the data you want to access as the object's data based on the contents of that variable.

      Common strategies include: just putting it in the scalar (for simple data), or array/hash (for more complex); and putting a key into some other data structure into the variable (inside-out uses package-level hashes, with the key being the blessed variable's address. Some people like to use arrays, for faster access.).

        For some reason that's the first explanation that's made sense :-P I will definitely have to do some more homework before I get to crazy, but my fingers are itching for a few practice problems.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://759051]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-07-22 09:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (453 votes). Check out past polls.

    Notices?