Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Global Vars

by Melly (Chaplain)
on Sep 04, 2009 at 13:31 UTC ( [id://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
    Melly:

    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?

    ...roboticus
      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

        tbone1:

        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:

        BEFORE

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

        ...roboticus
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found