Your first program doesn't work with warnings and strict turned on and I guess that this was what I asked in the original post (I was the original poster): the interpolation of $foo in your print function is illegal, since foo is only valid within the sub foo.
Also, you say that:
Defining a global variable would require you to do: my $foo in the main scope of a 'package'.
I guess that you meant declaring
a global variable, right?
It seems that using my $foo in package main doesn't seem to interfere with $main::foo anyway as can be seen in the following code snippet:
use strict; use warnings; use diagnostics;
my $foo = "hello"; $main::foo = "world";
print "$foo $main::foo\n";
(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 my $foo
under the package main
would manipulate the symbol table such that $foo
were equal to $main::foo
If $foo and $main::foo were the same variable, then the program would print world world, which it does not.
Furthermore, if you fully qualify your variables in package main, then you don't need to worry with declaring them with my, nor with our, nor with use vars, as can also be seen in the program above (see that $main::foo is not declared; yet, the compiler doesn't complain about it, even though the program is running under use strict).