Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-02-21 23:35 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.