First of all, the reasoning used in the quote doesn't really apply to hashes and arrays since they start out empty more often than none. That's probably why the quoted practice refers to undefined variables (which can only be scalars) instead of uninitialized variables. For example,

my @results; while (...) { ... push @results, ...; }

Secondly, I don't agree with the quoted practice for scalars either.

If it's that hard to tell if a variable was left undefined by accident, the solution isn't to add a useless initializer, it's to fix the documentation (maybe by adding a comment) or clean up the code (maybe by localizing your variable better). For example,

my $found = 0; my $match; # Only meaningful when $found is true. ...

If your goal is to avoid accidental omission of an initializer (as the quoted practice states), conditioning yourself to always add an initializer isn't going to help. You'll just substitute another problem (initializing your variable incorrectly) for the one you are trying to fix (forgetting to initialize your variables).

And honestly, how big of a problem is forgetting to initialize your variables? Warnings do an excellent job of finding those instances. The cost (clutter) doesn't warrant the price.

That said, I don't buy your coworker's argument either. I feel that initializing a variable using a BEGIN block on the next line is well within the sentiment of the quoted practice, so I think his counter-argument doesn't disproves anything. Tell me how anyone could think anything but "the lack of definition is intentional" in the following:

my $var1; my $var2; BEGIN { $var1 = ...; $var2 = ...; }

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