I'm working on a script and currently have a lot of global variables. Is it better to create a couple global hash key/values and incorporate the global vars into the hash?
Unlikely. What makes you think that's an improvement? "Don't have (too) many global variables1,2" is a good practice, but it's only useful if you know why. If you know why, you wouldn't consider putting them in a hash - it doesn't solve any of the problems associated with global variables, and introduces more problems.

Note it doesn't mean every global variable is "bad" - there are many cases where a global variable makes sense. And note there are many "hidden" global variables; in a naive, hashref based objects, its attributes are "global" as well - much more code can (unintended) touch them.

In doing so would this also speed up the script or slow it down?
Probably slow it down. But it's unlikely you'll notice the difference.

1 It's actually a misnomer. The potential problems have to do with variables having a too broad scope. Even if a variable isn't truely global, it can still have a scope that's too broad.
2 Some people think "global variable" means "package variable". It doesn't. A lexical variable can be a global variable as well. You don't solve the "don't have global variables" issue by just sticking a my in front of them!


In reply to Re: global variables vs global hash by JavaFan
in thread global variables vs global hash by swoop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.