No worries!

Actually, no, you don't need to prefix everything with "main::", though that would certainly be one way to go about it.

Better to make global only the few things that you really want to have global, by putting them at the top of your program, and prefixing them with my:

# My global variables (Actually, these should really be constants...) my $boiling_point_fahrenheit = 212; my $known_other_universes = 0; my $answer_to_everything = 42;
You should make everything else local (actually lexically-scoped) with my prefixing it.

A good test of whether a given variable should be global or not:  if you were convert most or all of the subroutines in the program into separate packages, and import them to the main program as objects, would the variable be one that you'd like to retain in the main program (global!), or pass to the constructor or one of the methods of some object? (lexical!)

So, go back and fix all of the errors that you get, and then let me know:  what kind of output do you get now??


In reply to Re^4: Understanding variable scope in perl by liverpole
in thread Understanding variable scope in perl by BioBoy

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.