in reply to Debugging "Use of uninitialized value" warnings.

I hope someone eventually answers this part of your question:

Is the best practice simply to code more defensively and explicitly check all variables are initialized prior to interpolation? I could do that, I guess, but I'm afraid of bloating the code needlessly.

because this drives me nuts, too. I am still very new to perl, at the stage where those warnings Do look like "noise" to me. I don't understand why I should have to explicitly set all my variables to a value to avoid this problem. If indeed, that's what I need to do. Particularly when the resulting output is what I want.

If anyone could point me toward some relevant pages here or in the Llama/ Camel books explaining this, I'd be very grateful.


  • Comment on Re: Debugging "Use of uninitialized value" warnings.

Replies are listed 'Best First'.
Re^2: Debugging "Use of uninitialized value" warnings.
by cdarke (Prior) on Apr 04, 2007 at 14:40 UTC
    3rd Edition of the Camel, first paragraph of chapter 4, p.111 discusses this.
    Perl does not force you to program in any particular style, but most experienced programmers have made enough mistakes to know that initialising variables, like use warnings and use strict, are a Good Thing.
    Strangely I can't find a direct 'Best Practice' to initialize variables in TheDamian's book.
Re^2: Debugging "Use of uninitialized value" warnings.
by ww (Archbishop) on Apr 04, 2007 at 14:25 UTC

    NovMonk: You'll need to read about warnings (perldoc -f warn) but this may also help.

    This generates a compile-time error if you access a variable that wasn't declared via use vars, localized via my or wasn't fully qualified.

    In (perhaps oversimplified) words, Perl is exhibiting a bit of DWIMery here; "protecting you from yourself" or "helping you catch mistakes like typos in a $var name (eg $var somehow is typoed as $vqr

    ps: take the advice at the top of that page, and use perldoc to obtain a fuller explation.