i am not completely certain i understand BEGIN{} blocks either, but let's take a look.
#!perl -l use strict; use warnings; print "v: $v"; BEGIN{ my $v }
gets us:
Global symbol "$v" requires explicit package name at begin.pl line 4.
BEGIN not safe after errors--compilation aborted at begin.pl line 5.
ok, it's barfing on the print line. looks like the BEGIN declaration isn't getting handled first, or if it is, it's not the right scope. let's try swapping the order of the lines to see if it makes a difference
#!perl -l use strict; use warnings; BEGIN{ my $v } print "v: $v";
gets us
Global symbol "$v" requires explicit package name at begin.pl line 5.
Execution of begin.pl aborted due to compilation errors.
different error. looks like our declaration isn't in scope. let's using "our" as you originally did, and try fully qualifying $v as $::v
#!perl -l use strict; use warnings; BEGIN{ our $v } print "v: $::v";
gets us
Use of uninitialized value in concatenation (.) or string at begin.pl line 5.
v: 
ok, so that works, it just doesn't like the fact that $::v isn't defined. now let's try defining $v outside the begin block
#!perl -l use strict; use warnings; my $v = 5; BEGIN{ $v++ } print "v: $v";
gets us
v: 5
ok, so there's still no problems with scope, but does the BEGIN code even get run? let's try something else
#!perl -l use strict; use warnings; my $v; BEGIN{ $v++ } print "v: $v";
gets us
v: 1
so the BEGIN code is getting run... it looks like BEGIN requires a definition and will use it even if it's in non-BEGIN code, but it gets first dibs on assignment. the reason we got v: 5 is because the assignment = 5 happened AFTER the begin block, even though the declaration and assignment happened in the same statement

fun stuff!

perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"


In reply to Re: Confusion about BEGIN block by pizza_milkshake
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.