First, let's get the incorrect terminology out of the way. Lexical and package variables are two different kinds of variables, and they are mutually exclusive. There's no such thing as lexical package variables.

>perl -le"package PkgA; my $foo = 'abc'; package PkgB; print $foo" abc

I usually use the term global variable for what you call lexical package variables. It's not perfect, but it's not outright wrong.

Now back to the subject. Why would you initialize a variable twice? That's bad, without or without BEGIN. If I saw code that looked like the following, I'd have a talk with you. And yet, that's exactly what you are doing.

my $stuff1; my $stuff2 = undef; my $stuff3 = undef; my $stuff4 = undef; $stuff1 = 'stuff1 stuff'; $stuff2 = 'stuff2 stuff'; $stuff3 = 'stuff3 stuff'; $stuff4 = 'stuff4 stuff'; print "stuff 1: $stuff1\n"; print "stuff 2: $stuff2\n"; print "stuff 3: $stuff3\n"; print "stuff 4: $stuff4\n";

You code without BEGINs should be

my $stuff1 = 'stuff1 stuff'; my $stuff2 = 'stuff2 stuff'; my $stuff3 = 'stuff3 stuff'; my $stuff4 = 'stuff4 stuff'; print "stuff 1: $stuff1\n"; print "stuff 2: $stuff2\n"; print "stuff 3: $stuff3\n"; print "stuff 4: $stuff4\n";

So your code should be

my $stuff1; my $stuff2; my $stuff3; BEGIN { $stuff1 = 'stuff1 stuff'; $stuff2 = 'stuff2 stuff'; $stuff3 = 'stuff3 stuff'; } my $stuff4; INIT { $stuff4 = 'stuff4 stuff'; } print "stuff 1: $stuff1\n"; print "stuff 2: $stuff2\n"; print "stuff 3: $stuff3\n"; print "stuff 4: $stuff4\n";

Of ir you had anything complex, an adherent to the quoted practice would do

my $stuff; BEGIN { $stuff = undef; ... ... ... ... Some complex code to initialize $stuff. ... ... ... }

instead of

my $stuff; BEGIN { ... ... ... ... Some complex code to initialize $stuff. ... ... ... }

In reply to Re^2: Use of uninitialized variables? by ikegami
in thread Use of uninitialized variables? by Zadeh

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.