Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

phillup (4419)

phillup
  (email not shown publicly)
http://www.philsplace.org/

Journal of phillup (4419)

Thursday August 12, 2004
06:25 PM

Warning Will Robinson

[ #20384 ]

So, about a month ago I got all into writing test code for my modules. But, we are talking a *LOT* of code... so, I'm spending an hour or so a day to write tests of the existing code... and the rest of the day I write tests before writing new code.

I figure I'll get caught up an a year or two.

;-)

Anyways... one of the test that I wrote checked each module file (and all of my maintenance scripts) for 'use warnings'.

So, it took a day to fix all of the files and the warnings... proof positive of the need for unit testing.

Still, since I'm starting so late in the process and taking a 'piecemeal' approach to getting full test coverage (that'll be the day!)... I'm coming across many places where I get a particular warning:

Use of uninitialized value in...

Now, one hundred percent of the time (so far) that I have gotten the warning, my code has been "reasonable". In other words... there were not any "unintentional" use of the variables. For example:

my $sum;
foreach (keys %scores){
  $sum += $scores{$_};
}

Will trigger the warning, since I'm using sum without explicitly setting the value to zero before hand. But, this is Perl, and I know that it will do that for me. And... I'm lazy. Worse, I don't see the need to do it twice (once by Perl, once by me).

And, since I believe that any variable that is lexically scoped via 'my' should be considered initialized... that is just salt in the wound to me.

So... I've been wrapping those small chunks in their own blocks and doing the 'no warnings "uninitialized"' thing... But... I'm leaning towards simply making that the default for the entire module(s).

Are there any warnings that you or your company ignore as "standard practice"?

And, of course, what are the pitfalls of doing so...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • my $sum;
    foreach (keys %scores){
      $sum += $scores{$_};
    }
    Any 'uninitialized' warning there isn't because $sum is uninitialized. Perl handles that fine without warning. Now, if one of the values of %scores is undefined, that's another matter :)
    • <hitting self on head>

      Of course.

      Hm... need to look closer at the code...

      My "real" code involved string concatenation... and yes, some of the hash values are 'undef' which leads to the warning.

      I didn't take that into account when writing this up.

      Thank you.
  • Do not ignore warnings.

    Do NOT ignore warnings. The whole point of automated tests is so that you, the human, doesn't have to think about things. If you have to think "I can ignore this one, I can't ignore that one", then you're thinking.

    Worse, if you ignore them, then one day there'll be another "real" warning that will be a bad thing, and you'll ignore it.

    All warnings are errors.

    --

    --
    xoa

    • Do not ignore warnings.

      Yes, I've read your slides. And, I agree. Very much.

      Which, is part of the quandry. I don't want "false" warnings drowning out the "real" ones.

      But...

      My gut tells me that all of these are going to be of the same class of "problem" where my programming style, doing things like expecting an undefined value to equate to zero (actually, most of it is string concatenation)... is what is causing the warning.

      So... do I hunt down each individual case... change my style of programming (mig
      • I guess it's something about your style of programming. I make use of Perl's features all the time, and I rarely run into "uninitialized" warnings. The places where I expect to be able to have undef automagically convert to '' or 0 are the places where Perl doesn't warn, but maybe I internalized Perl's ideas about when to warn a long time ago. The vast majority of times when I get an "uninitialized" warning, it's indicating a real error.

        In your example, I'd have expected that undefined values in %scores we