Certainly read the tutorials linked by AnonyMonk above.

Then consider the following code:

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; package P; ;; $P::A = 'something'; ;; package main; ;; my $A = 123; $::A = 456; ;; print 'package ', __PACKAGE__; print qq{\$A is $A}; print qq{\$::A is $::A}; print qq{\$main::A is $main::A}; print qq{\$P::A is $P::A}; print qq{\n}; ;; our $A; print 'package ', __PACKAGE__; print qq{\$A is $A}; print qq{\$::A is $::A}; print qq{\$main::A is $main::A}; print qq{\$P::A is $P::A}; " "our" variable $A masks earlier declaration in same scope at -e line 1 +. package main $A is 123 $::A is 456 $main::A is 456 $P::A is something package main $A is 456 $::A is 456 $main::A is 456 $P::A is something
In this code:
  1. The package (or namespace) P is declared and the package variable $P::A is defined and initialized in it. This variable is autovivified by the assignment operation. strict is happy with this assignment because the package variable name is fully qualified.
  2. The package (or namespace) is switched to main and a lexical variable $A is defined and initilized, and a package variable $main::A in the main namespace is defined and initialized. (The shorthand form of the fully qualified name $main::A is used.) The lexical variable is "visible" until the end of its lexical scope: the end of the program in this case although it might have been given a much narrower scope. The package variable is visible from any scope and any module; it is truly global.
  3. The set of print statements shows that the lexical variable $A and the package variable $main::A (or $::A) are independently accessible.
  4. The  our $A; statement is executed. our creates a lexical alias of the $A symbol to the package variable $main::A for the remainder of the lexical scope. I have taken this expression out of the print statement and made it a separate statement to more clearly illustrate how it works. The alias of the symbol $A to $main::A means that the original lexical variable (the my variable) named $A is no longer "visible", i.e., accessible. That's what the warning is about.
  5. Another set of print statements identical to the first illustrates that $A is now indeed the same as $::A and $main::A.
  6. In all the print statements, the $P::A variable is always fully accessible because it is always fully qualified.


Give a man a fish:  <%-{-{-{-<


In reply to Re: how to declare a package variable by AnomalousMonk
in thread how to declare a package variable by scigeek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.