Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Global Vars

by tbone1 (Monsignor)
on Sep 04, 2009 at 13:57 UTC ( #793461=note: print w/replies, xml ) Need Help??

in reply to Re: Global Vars
in thread Global Vars

What he said. I have sometimes created a global hash (called something like %my_global_values or something like that) so that 1) it's obvious what is a global variable, and 2) I'm far less likely to accidentally reuse a variable.

And the quick and dirty programs probably last longer because expectations are lower, they're for (relatively) simple things, and developers are less likely to overthink them.

tbone1, YAPS (Yet Another Perl Schlub)
And remember, if he succeeds, so what.
- Chick McGee

Replies are listed 'Best First'.
Re^3: Global Vars
by roboticus (Chancellor) on Sep 04, 2009 at 15:01 UTC

    Nice technique. It seems to me that it's a handy step on the way to splitting things off into modules and/or object-oriented bits. Since all the references go through your global hash anyway, when you split off a function/module/object, the code in your subroutines wouldn't have to change much:


    my %my_global_values = (foo=>0, etc=>0); my $rGlobals = \%my_global_values; sub zigafoo { $rGlobals-> = 7 }
    package barbaz; my %my_package_values = (foo=>0); my $rPackage = \%my_package_values; sub foobar { $rPackage->{foo} = 7; }


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://793461]
[Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )
[talexb]: TStanley I just realized that I was in your area for a convention back in April. We were at the high school for the weekend, the one with the long driveway. Nice spot.
[TStanley]: I am heading to Arlington, Virginia next week, then I get to spend an entire week at home. Looking forward to that as I will have spent 7 weeks on the road by the end of next week
[talexb]: That's a long road trip. I haven't travelled for business in years.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-06-23 14:21 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (548 votes). Check out past polls.