perlquestion
OzzyOsbourne
<p> I looked at the docs, looked at some threads, delved into the CB, and consulted the magic 8 ball, but I still can't seem to get my head around the answer to:</p>
<p><i>What is the difference between my, use vars, and our?</i>
<p>Most answers seemed vague, or I just didn't understand. The most worrysome answer was "Outlook not so good" from the magic 8-ball. I'm an NT admin. I <I>know</I> about Outlook not so good. Tell me about Perl scope, 8-ball. Perl scope!</p>
<p>What I think I know:</p>
</i></p>
<ul><li><code>my</code> declares variables in lexical scope. Yup, got it.</li>
<li>use vars allows you to declare global variables, and refer to <code>$package::$foo</code> as <code>$foo</code>. Right on.</li>
<li>and <code>our</code> is the same as use vars but with lexical scope. A globally lexically thingy. OK.</li>
</ul>
<p>So on line 4 or 5, before any blocks, I declare my $foo. It acts with a lexical scope of the entire script. It acts sort of like a global variable.</p>
<p>What if I put our $foo in the same place. Do I get the same effect? What if I put use vars $foo?</p>
<p>My questions:</p>
<ol>
<li>Is there any difference between declaring variables at the beginning of scripts with <code>my</code> or <code>our</code>? Won't their scope be the same?</li>
<li>Is there a reason to use one or the other in this situation</li>
</ol>
<p>See this [id://106106|example]. Look at <code>%TOC</code>. Should it be <code>my</code> or <code>our</code>?</p>
<p>Thanks for your help.</p>
<p>-<a href="/index.pl?node=OzzyOsbourne&lastnode_id=1072">OzzyOsbourne</a></p>