our is a lexically scoped alias for a global variable. Will create the global if it didn't exist already.
See the following code:
#!/usr/bin/perl
our $a = "foo";
print "$a\n";
package Our;
print "$Our::a <--shouldn't print anything here\n";
print "$main::a <--should print 'a' from the main symbol table here\n"
+;
print "$a <--should also print 'a' from the main symbol table he
+re\n\t\tdue to lexically scoped alias\n";
Results are:
foo
<--shouldn't print anything here
foo <--should print 'a' from the main symbol table here
foo <--should also print 'a' from the main symbol table here
due to lexically scoped alias
By default any global variables you create automatically populate the "main" symbol table,
%main::. With a package declaration you could change which symbol table you are populating when you declare unqualified variables.
But our is a lexical decalaration. In the case above the declaration was file scoped. That's why I still got something back when I gave it an unqualified print statement
print "$a\n" after the package declaration.
Don't forget just because it's a lexically scoped variable, it's still an alias for a global variable. Hence when I reassigned $a = "foo" with the our declaration, I blew away the old value.
Any comments are welcome.
Cheers,
Kristina
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.