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

Having been totally led astray by the enigmatic "Use of uninitialized value in string eq" warning, I am now boggled.

This snippet of code gives the warning "Use of uninitialized value in string eq"

my %data = @_; warn $data{'FLOATER'}; print $data{'FLOATER'}."<-----------"; if( $data{'FLOATER'} eq 'Y' ){
This is in a subroutine, hence the use of @_. The warn and print statements are for debugging this warning, and both show this hash key to hold the value "N". A defined($data{'FLOATER'}) returns true. What am I missing?

I have searched for similar problems with this error string, but it is so common that I haven't found anything useful. I have tried different combos of quotes around the hash key, and the constant 'Y'. Nothing seems to matter. I have also seen the same warning with similar code, but different values.

Replies are listed 'Best First'.
Re: Confusing Uninitialized warning
by kaif (Friar) on Jun 13, 2005 at 16:24 UTC

    Do you have an elsif or such construct below your posted code? Sometimes Perl reports "strange" locations for errors involving these constructs. See No Pause on Elsif in Debugger for more information.

      kaif, You've hit the nail on the head. I guess if an error occurs in elsif statements it reports the line number of the parent if statement. Not very helpful. Thanks, Spencer
Re: Confusing Uninitialized warning
by ikegami (Patriarch) on Jun 13, 2005 at 16:18 UTC

    Maybe the line number in the error message is wrong. That happens (particularly when the error is in an elsif). If defined($data{'FLOATER'}) is true as you say, it makes no sense for the if to give you that warning.

    Update: Additionally, you're probably better off passing the hash by reference. It saves memory, executes faster and preserves tie magic. For example:

    sub test { my ($data) = @_; ... print($data->{'FLOATER'}); # Need to add "->" ... } my %data = ...; test(\%data);
Re: Confusing Uninitialized warning
by Joost (Canon) on Jun 13, 2005 at 16:20 UTC
    That snippet is perfectly fine. Your problem is somewhere else. Please post code that reproduces the problem. Something other than the following:
    #!/usr/bin/perl -w use strict; test( FLOATER => 'N'); sub test { my %data = @_; warn $data{'FLOATER'}; print $data{'FLOATER'}."<-----------"; if( $data{'FLOATER'} eq 'Y' ){ print "Test: yes\n"; } } __OUTPUT__ N at test.pl line 8. N<-----------
Re: Confusing Uninitialized warning
by davidrw (Prior) on Jun 13, 2005 at 16:21 UTC
    is that the _only_ warning given? What does print join ':', @_; show (so we can see what's in there)? Or may the context in which the sub is being called? This code works fine for me w/no warnings:
    use strict; use warnings; my %data = ( FLOATER => 'N', BLAH=>2); warn $data{'FLOATER'}; print $data{'FLOATER'}."<-----------\n"; warn 'ok' if $data{'FLOATER'} eq 'Y';