I've been trying to figure out how to how to write my code so I never get those annoying "uninitialized value" warnings that appear far too frequently for me.

If you're getting these frequently, then you have enough data to look for patterns. Without knowing what the underlying problem patterns are, you run the risk of hiding problems deeper by, for example, always manually initializing variables.

Getting an "uninitialized value" warning tells you that you managed to get to a specific place in your code without having a value assigned to a variable (or element of an array or hash) at a value is now needed. (That's the "well, duh" part.) What's more interesting is why?

Better, I think, to seek first to understand why you're having this problem, rather than reaching immediately for a coding solution.

I had a problem early in my career with a certain pattern of conditional logic. I'd try to write code in a certain way, and my brain would basically hit a blind spot. Debugging was a bitch, because my brain wasn't untangling what it was seeing. Only by stopping to analyze the problem (with some help) was I able to make it go away.


In reply to (dws)Re: on to better coding (no uninitialized values) by dws
in thread on to better coding (no uninitialized values) by melguin

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.