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).
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||