Hey folks, co-worker here. :) Here's a particular, extremely simplified example of what I'm talking about. The example uses scalars, but applies equally to hashes and arrays. I'm specifically talking about lexical package variables, usually used in the case of inside-out classes. We start by setting up a few variables, in this case, scalars, and then initialize them in various places. We're using undef, but it could be other values. Then, a user of the module or class creates/initializes these values in a begin block of their own, and we see what happens.
use strict; use warnings; my $stuff1; my $stuff2 = undef; my $stuff3; my $stuff4; BEGIN { $stuff3 = undef; } INIT { $stuff4 = undef; } BEGIN { # hypothetical initialization, creation going on. $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";
And the results are:
stuff 1: stuff1 stuff
Use of uninitialized value in concatenation (.) or string at beginproblem1.pl line 47.
stuff 2:
stuff 3: stuff3 stuff
Use of uninitialized value in concatenation (.) or string at beginproblem1.pl line 49.
stuff 4:
By assigning 'empty' values to these internal package variables, you end up overriding them if your package or class is used in a BEGIN or INIT block. We're using perl v5.8.8.

In reply to Re: Use of uninitialized variables? by Glav
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.