I thought it gave it a global symbol a local value?
It does, at run-time.
- Foo.pm gets compiled.
- Foo.pm's topmost level gets executed.
- local $Foo::bar gets executed.
- $Foo::bar current value (undef) is saved.
- $Foo::bar is set to undef.
- Anything else in Foo.pm's topmost level gets executed.
- $Foo::bar reverts to undef. A file counts as block for scoping purpose, and we've just run to the end of the block in which local $Foo::bar was called.
- &Foo::init is called.
- &Foo::init sets $Foo::bar to 42. There's no local $Foo::bar in effect, so the value remains.
What is the purpose of the declaring $Foo::bar local?
None, unless it's used in Foo.pm's topmost level, or in a function (directly or indirectly) called from Foo.pm's topmost level.
If so, when init is called will it set the localized value like a closure or the global value?
It depends on whether someone in the call stack did local $Foo::bar. Unless &Foo::init is called from Foo.pm's topmost level (where there is such a local), probably not.
Further, what is the point of qualifying the identifier with the package name? and does that have any effect on the functionality?
Removing it will result in a compilation failure because use strict 'vars' is active. If you want to remove the need for the package quantifier, use our $bar; in scope at compile-time.
use strict;
use warnings;
package Foo;
our $bar; # "our" is like "my", but it
# creates a package variable
# instead of a lexical variable.
sub init {
$bar = 42; # "our" removes the need to
# specify the package name.
}
...
1;
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.