I think I'm starting to get the hang of this, thankyou.

I guess my greatest confusion was where and when symbol names needed to be declared. Mmmm.

I think I understand that

1. our $var is lexically scoped, and is just an alias for $__PACKAGE::var

2. because our $var is lexically scoped, when it is in a BEGIN block, it does not mean that the alias (i.e. $var) is available outside of the BEGIN block.

3. however, if the alias is defined outside the BEGIN block, it will be available for use within the BEGIN block because of the lexical scope if and only if it is declared before the BEGIN block is written

EXAMPLE #1 works, #2 does not

#!/usr/bin/perl use strict; our %aa; our $bb; our @cc; print keys %aa,", $bb, @cc\n"; BEGIN {%aa=(1,11); $bb=22; @cc=(3,33); }
#!/usr/bin/perl use strict; BEGIN {%aa=(1,11); $bb=22; @cc=(3,33); } our %aa; our $bb; our @cc; print keys %aa,", $bb, @cc\n";
4. all code within the BEGIN block is executed first, regardless of it's location within the file

5. egads! no wonder I'm confused


In reply to Re^2: Confusion about BEGIN block by Sandy
in thread Confusion about BEGIN block by Sandy

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.