Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Global Vars

by roboticus (Chancellor)
on Sep 04, 2009 at 13:36 UTC ( [id://793454]=note: print w/replies, xml ) Need Help??


in reply to Global Vars

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

Replies are listed 'Best First'.
Re^2: Global Vars
by tbone1 (Monsignor) on Sep 04, 2009 at 13:57 UTC
    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

Log In?
Username:
Password:

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

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

    No recent polls found