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

I get the warning "Use of uninitialized value at .." when I check the return value from a DBI select statement against "undef" in order to set it to 0 if NULL is returned. Please advise. Thanks, ICG

Replies are listed 'Best First'.
Re: Warning for checking with undef
by splinky (Hermit) on Jul 07, 2005 at 13:10 UTC
    You don't say exactly HOW you check the return value against undef, but I'm guessing you're doing something like "if ($val eq 'undef')" or "if ($val == undef)", both of which are wrong.

    The correct way is "if (defined $val)". This won't trigger any warnings.

Re: Warning for checking with undef
by mrborisguy (Hermit) on Jul 07, 2005 at 13:09 UTC

    From the statement "check the return value ... against 'undef'," do you mean you are doing something like:

    my ($row1,...) = $sth->selectrow_array; if( $row1 == undef ) { $row1 = 0; } # or $row1 = 0 if $row1 == undef;
    If you are, you may want to try something like:
    if( not defined $row1 ) { $row1 = 0; } # or $row1 = 0 if not defined $row1;

        -Bryan

Re: Warning for checking with undef
by Hena (Friar) on Jul 07, 2005 at 13:11 UTC
    Check out function defined(). Example.
    if (!defined($foo)) { }
Re: Warning for checking with undef
by anonymized user 468275 (Curate) on Jul 07, 2005 at 13:44 UTC
    Whereas testing defined($var) is arguably better style; I feel I should point out that the use of undef shown in the responses above is wrong for the wrong reason with respect to the OP.

    undef needs to be parsed as a function call in this context, so when used in the context of a relational expression, it needs round brackets after it to prevent it from being treated as a bareword. i.e.

    if ( $foo == undef() )

    would work just as well as

    unless( defined($foo) )

    whereas

    if ( $foo == undef )

    fails to satisfy for the same reason as

    if ( $foo == DoesntMatterIfIamADefinedFunctionOrNot )

    The same programming error is often made with shift();

    One world, one people

      Erm, undef without parens returns a perfectly valid value in that context. The problem is that it's being coerced into a numeric value because of the == which triggers the warning.

      What you're thinking of is using undef or shift as a hash key ($h{undef}) or to the left of a => (%a = ( undef => 1 )) where there is some bareword magic going on (strict or no). In those cases it is necessary to use parens or a + to disable the magic.

      Update: And you can verify it by looking at the different errors produced. Bareword-ness doesn't enter into it.

      freebie:~ 746> cat baz use warnings; if( 5 == undef ) { 1 } if( 5 == "undef" ) { 1 } freebie:~ 747> perl baz Argument "undef" isn't numeric in numeric eq (==) at baz line 3. Use of uninitialized value in numeric eq (==) at baz line 2.

      The first one gripes about comparing against undef, the second because you're comparing a non-numeric value numerically.

      --
      We're looking for people in ATL

        I am testing on 5.6.1 and will have to look into it again, because my results look different from others.

        One world, one people

          A reply falls below the community's threshold of quality. You may see it by logging in.