Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Global Vars

by Melly (Hermit)
on Sep 04, 2009 at 13:31 UTC ( #793453=perlquestion: print w/ replies, xml ) Need Help??
Melly has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monkeys,

I've got a big chunk of code for some data-transformation. Basically, it reads in some mapping information, some config options, and then processes a hefty data-file.

This data-file consists of n number of lines per subject, and the process handles each subject, reading, translating and outputting data, and then moves on to the next subject.

Now, all the subject-specific stuff uses local variables, and passes them to sub-routines in the approved fashion.

However, the mapping and config options are all stored in global vars.

How much of this is a no-no IYHO? (ONSHO?)

map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk

Comment on Global Vars
Download Code
Replies are listed 'Best First'.
Re: Global Vars
by roboticus (Chancellor) on Sep 04, 2009 at 13:36 UTC

    For quick & dirty stuff, I don't mind globals a bit. They get to be a problem when the program starts to get too large and complicated. If you ever find yourself using grep to track down usage of a global variable, or when you have a quick reference sheet for your global variables, or when you track down a bug due to reusing a global variable name that you forgot, then you know that your program is past due for some reorganization/refactoring/etc.

    I always wonder why quick & dirty programs last longer than the supposedly long-term ones?

      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


        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; }

Re: Global Vars
by JavaFan (Canon) on Sep 04, 2009 at 14:52 UTC
    It depends what you use the variable for. If you use it globally, by all means, use a global variable. That's, IMO, cleaner than passing the same variable to (almost) every function. It's even worse if you have 12 such variables.
      Thanks all - pretty much confirms what I thought. Phew.
      map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
      Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://793453]
Approved by tbone1
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-11-28 20:15 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (744 votes), past polls