kepler has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've got a Perl script that is runned by a cron job. I get an email every 6 hours telling me:
Use of uninitialized value in substitution (s///) at /home/rui/public_ +html/cgi-bin/e/e_update.pl line 49. Use of uninitialized value in length at /home/rui/public_html/cgi-bin/ +e/e_update.pl line 53.
Here's line 36 to 57:
my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j) = split (/\,/,$line); $a = $a.$c.$i.$b; if ($e < 0){ $e = abs($e)."S"; } else { $e = abs($e)."N"; } if ($f < 0){ $f = abs($f)."W"; } else { $f = abs($f)."E"; } $d =~ s/\;/\,/gi; $j =~ s/\;/\,/gi; #line 49 my ($k,$l) = split (/\;/,$h); if(length($l) > 0){ #line 53 print FILE $d."|".$g."|".$i."|".$k."|".$e.", ".$f."\n"; } else { print FILE $d."|".$g."|".$j."|".$k."|".$e.", ".$f."\n"; }
Any idea how to stop this? Kind regards, Kepler

Replies are listed 'Best First'.
Re: "Use of uninitialized value" annoying message
by GrandFather (Saint) on Feb 21, 2011 at 04:46 UTC
    Any idea how to stop this?

    Several ideas, most of which you wouldn't like. However, the implication is that $line sometimes doesn't contain the $j field. You could provide a default value for it if that is appropriate, or take some other action such as ignoring the line and writing an error to your error log.

    The important point is that Perl is telling you that you haven't handled an unexpected condition and that you need to take appropriate action.

    As an aside, your variable names are awful and your indentation is pretty bad too! Note that $a and $b are special variables and their use outside of a sort block will sometimes bite you on the bum.

    $d."|".$g."|".$j."|".$k."|".$e.", ".$f."\n" is better written: "$d|$g|$j|$k|$e, $f\n".

    True laziness is hard work
Re: "Use of uninitialized value" annoying message
by Anonymous Monk on Feb 21, 2011 at 04:49 UTC
    splain/diagnostics If you use warnings; or -w, you get warnings. If you're confident your program is working as it should, you could add no warnings 'uninitialized'; to silence only these warnings, or you could simply not turn on warnings.

    Or you could turn any undefined values from split into empty string

    ... $j) = map { defined ? $_ : '' } split ...
    Its much less work to let Text::CSV handle CSV
Re: "Use of uninitialized value" annoying message
by educated_foo (Vicar) on Feb 21, 2011 at 04:38 UTC
    Add
    no warnings 'uninitialized';
    to your script.

    EDIT

    Ah, Perlmonks...

    OP: How do I get rid of this warning?
    Me: Do this.
    OP: Thanks.
    10 monkeys: LEPER OUTCAST UNCLEAN!
    GrandFather: (long-winded irrelevant scolding)

      Thank you very much. Kind regards, Kepler

        Unless you understand why the warning is being generated and are sure that undef is a valid value you should not turn the warning off. Even if you do decide to turn off the warning, you should constrain the scope of no warnings to the smallest scope possible:

        { no warnings 'uninitialized'; $j =~ s/\;/\,/gi; }

        However it's not at all clear from your code that turning off the warning is appropriate. Make sure you understand what's going on before you opt for that solution!

        True laziness is hard work