As a matter of practice, I've always saw to it that any variables I declared were initialized. That includes scalars, hashes, and whatever else:
my $retval = 0; my @list = (); my %hash = ();
In Perl Best Practices (PBP) section 5.2, there is talk of initializion but only in the context of locals:
"Even if you specifically did want that variable to be undefined, it's better to say so explicitly: ... That way any readers of the code can immediately see that the lack of definition is intentional, rather than wondering whether it's an oversight."
What about the general case? Arguably, it's not of much significance in perl compared to other languages since your data will just get the value undef, but I'd rather not have to deal with undef or all the defined() checks on variables. I'd rather just assume, in my own code at least, that something undef indicates a problem somewhere. During a recent code review this issue came up, and a fellow co-worker argued against this practice because:
Outside subroutines, there's a big difference. As package variables, the assignments happen at certain times. For example, you have a package variable that gets assigned in a subroutine. You have no idea when that subroutine is called; it can be called from a BEGIN {} block. Then later, if you have that package variable assigned during a regular block, the values get overriden by my @var = ();. The = () happens during the normal execution time but the my @var exists before the BEGIN block so you end up wiping out everything you put in your package variables this is really bad if you're doing inside-out classes.
Putting aside the issue of whether one ought to be using inside-out classes, what do you perl monks find to be the best practice?

In reply to 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.