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

i am doing something like this
my ($moh) if(<something>) { $moh="mohanish"; } if(<something-else>) { if($moh eq "mohanish") { <--- i am getting error in this line } }
This is the error :
Use of uninitialized value in string eq ....
Worst Part is that it is working fine in debugger
i.e if in debugger i do
x $moh eq "mohanish" i just get a output ''

Replies are listed 'Best First'.
Re: Use of uninitialized value in string eq
by ccn (Vicar) on Oct 23, 2008 at 06:14 UTC
    In a case not <something> the $moh variable remains uninitialized. Just initialize it in declaration line
    my $moh = ''; # note that parens are not required here
      Curious.. Doing that makes Perlcritic http://www.perlcritic.com/ spit out a Severity 2 error "Quotes used with an empty string .. See p53 of PBP" ... wonder why this is?

      Anyone have PBP on their shelf and can look on p53, or already know the more explicit reason ?

Re: Use of uninitialized value in string eq
by lamp (Chaplain) on Oct 23, 2008 at 06:20 UTC
    Please note that what you getting is a warning, not an error. Warnings alert you of situations that can lead to possible problems, but they do not terminate your programs like errors do. For eg.

    my $string; # $string is declared but not initialized!

    so any operation you do on '$string' (except an assignment) will produce the warning message, if you use 'use warnings' or -w option.

    check the value of "$moh" variable before comparing.

      Please note that what you getting is a warning, not an error

      I've heard this a few times this week and it's just plain wrong. They're not (immediately) fatal errors, but warnings can (and often do) indicate errors. I even say they should be treated as errors until examined, understood, ruled benign and silenced.

      "Please note that what you getting is a warning, not necessarily an error."

Re: Use of uninitialized value in string eq
by brsaravan (Scribe) on Oct 23, 2008 at 06:52 UTC
    You are trying to compare with an uninitialized string... Do all those if it is defined.
    if (defined $moh) { ..... }
      You are trying to compare with an uninitialized string... Do all those if it is defined. if (defined $moh) { ..... }

      That is just treating the symptoms rather than the root cause of the problem. Much simpler and safer, surely, to initialise,

      my $moh = q{};

      before use.

      Cheers,

      JohnGG