Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Scope is just not for Fresh Breath

by notsoevil (Pilgrim)
on Dec 28, 2000 at 22:39 UTC ( [id://48657]=perlquestion: print w/replies, xml ) Need Help??

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

With warnings (-w) on, the following code produces a 'use of uninitialised value in join' error:

my @data = (); while (@data = $sth->fetchrow_array){ print TSCH join(' ',@data),"\n"; }

The above was what I attempted after first using (which produced the same error):

while (my @data = $sth->fetchrow_array){ print TSCH join(' ',@data),"\n"; }

perldiag says:

Use of uninitialized value%s (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

The while loop keeps looping until the expression evaluates to false. The fetchrow_array method returns an empty list when there is no more data, and Perl treats that as a false value; and so the loop stops. So I cannot see why join would see @data with an uninitialized value.

I am sure I am overlooking something obvious, but perhaps not. I, a humble acolyte, seek your wisdom.

--
notsoevil
--

Replies are listed 'Best First'.
Re (tilly) 1: Scope is just not for Fresh Breath
by tilly (Archbishop) on Dec 28, 2000 at 22:55 UTC
    Try this:
    while (my @data = $sth->fetchrow_array){ print TSCH join(' ', map {defined($_) ? $_ : 'NULL'} @data),"\n"; }
    The NULLs you see were the cause of your warning.
      Thank you, that is much appreciated.

      --
      notsoevil
      --

Re: Scope is just not for Fresh Breath
by chipmunk (Parson) on Dec 28, 2000 at 22:55 UTC
    The DBI module converts SQL's NULL value into Perl's undef, and vice versa. You're getting a warning because one of the rows you're fetching has a column with a NULL value, which gives undef for one of the elements in @data. (For example, @data might be (7, 'Bob', undef, 'bob@example.com'). You could simply turn off warnings local to the block, since the warning isn't pointing out an actual problem.

    This conversion between NULL and undef can also be seen with $dbh->quote(undef), which returns the string 'NULL'. undef can also be used with placeholders to get a NULL value in SQL.

Re: Scope is just not for Fresh Breath
by 2501 (Pilgrim) on Dec 28, 2000 at 22:51 UTC
    I've had problems with fetchrow before if you are using a SQL function. i.e.:
    select max(price) from foo where bar
    In that case, fetchrow always returns something which triggers any loops to be true, but the value of what it returned could be null or intended as false. The max() function is somehow screwing with it. If I were to remove max() it would fail the loop.
    That problem caused much hair to be lost on my part:)
    I would be interested in seeing the query.
Re: Scope is just not for Fresh Breath
by adamsj (Hermit) on Dec 28, 2000 at 22:58 UTC
    I like tilly's suggestion that there are undef values in your array as a result of null values.

    Since I have neither Stein's book or a box for testing at hand, I'll have to just throw out another suggestion: If you only receive one warning, does what does fetchrow_array assign to @data when the statement evaluates to false?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://48657]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found