Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Are global variables "bad"?

by DStaal (Chaplain)
on Apr 21, 2009 at 16:36 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://759051]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-19 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found