And if the othe various posts haven't illuminated yet, it's worth walking through some other examples step by step. (Including strict and warnings.)

Using all fully qualified globals:

use strict; use warnings; $main::var = 1; { $main::var = 2; print $main::var, "\n"; # prints 2 } print $main::var, "\n"; # also prints 2

Declaring within the block:

use strict; use warnings; $main::var = 1; { our $var = 2; print $var, "\n"; # prints 2 } print $main::var, "\n"; # also prints 2

Declaring within the block and trying to use unqualified later (on line 9) causes a compile-time error under strict -- the "our" only has its effect of allowing use of the variable name without fully qualifiying it while within the block:

use strict; use warnings; $main::var = 1; { our $var = 2; print $var, "\n"; # prints 2 } print $var, "\n"; # also prints 2

Typical usage "at the top" applies to the whole file:

use strict; use warnings; our $var = 1; { $var = 2; print $var, "\n"; # prints 2 } print $var, "\n"; # also prints 2

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: my() and our() by xdg
in thread my() and our() by dsb

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.