note
rbrito
<p>Your first program doesn't work with <code>warnings</code> and <code>strict</code> turned on and I guess that this was what I asked in the original post (I was the original poster): the interpolation of <code>$foo</code> in your <code>print</code> function is illegal, since <code>foo</code> is only valid within the sub foo.
<p>Also, you say that:
<blockquote>Defining a global variable would require you to do: my $foo in the main scope of a 'package'.</blockquote>
I guess that you meant <em>declaring</em> a global variable, right?
<p>It seems that using <code>my $foo</code> in package main doesn't seem to interfere with <code>$main::foo</code> anyway as can be seen in the following code snippet:
<code>
#!/usr/bin/perl
use strict; use warnings; use diagnostics;
package main;
my $foo = "hello"; $main::foo = "world";
print "$foo $main::foo\n";
</code>
(Isn't it weird? It is terrible to think one knows the language after reading a lot about it, but still fail to understand things which should be simple semantics of the language. I would guess that the <code>my $foo</code> under the <code>package main</code> would manipulate the symbol table such that <code>$foo</code> were equal to <code>$main::foo</code>).
<p>If <code>$foo</code> and <code>$main::foo</code> were the same variable, then the program would print <code>world world</code>, which it does not.
<p>Furthermore, if you fully qualify your variables in package main, then you don't need to worry with declaring them with <code>my</code>, nor with <code>our</code>, nor with <code>use vars</code>, as can also be seen in the program above (see that <code>$main::foo</code> is not declared; yet, the compiler doesn't complain about it, even though the program is running under <code>use strict</code>).
149304
149306