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

I have a script that crashed a production web server yesterday.
$siteslisted=0;
This loop spun forever.

while ((($index, $name, $address, $zip)=$sth->fetchrow_array) && $siteslisted<$sitesperpage){
# other stuff
$siteslisted++;
}
This one works. while (($index, $name, $address, $zip)=$sth->fetchrow_array){
# other stuff
$siteslisted++;
if ($siteslisted<$sitesperpage) {last;}
}
Can some monk explain to me what I did wrong?
I'm betting it's a stupid mistake like operator precedence,
but I would rather ask and be enlightened before taking
down a production machine.

Replies are listed 'Best First'.
Re: Boolean operators and list assignments
by chipmunk (Parson) on Dec 20, 2000 at 22:30 UTC
    I'm actually not sure this is a problem with precedence or with list assignment. Note that the list assignment is in scalar context in both code blocks. The first is in the scalar context of &&; the second is in the scalar context of a while condition.

    However, your two loops have different ending conditions; the first ends when $siteslisted >= $sitesperpage, while the second ends when $siteslisted < $sitesperpage. That may just be an error in retyping the code here, though.

    Based on the code you posted, it's hard to say what the problem is...

      Sorry that was a typing problem.
      it should be if ($siteslisted >= $sitesperpage) {last;}
      I am really confused, because I'd expect
      that a expression that can be used at a boolean on it's own
      should be usable with a boolean operator.
      "Sometimes it's hard to tell the dancer from the dance."-Corwin in CoC

        So are we (confused). You should probably do that standard work that you should always do when you run into a problem:

        Take the code that produces the problem and reduce it to a minimal example that reproduces the problem. You will likely discover the source of the problem when doing this. If you don't find the solution, then post exactly the very small script that reproduces the problem along with the exact input, output (including error messages), and what about the output you didn't like.

                - tye (but my friends call me "Tye")
        Okay, that clears up that little issue.

        I tried both of your code structures, and they both worked fine, stopping after $sitesperpage fetches from the database. I wonder if there is a bug in the actual code that isn't reflected in the posted code. Perhaps a mispelled variable name or something like that? Are you using -w and use strict?

Re: Boolean operators and list assignments
by boo_radley (Parson) on Dec 20, 2000 at 22:25 UTC
    while ((($index, $name, $address, $zip)=$sth->fetchrow_array) && $site +slisted<$sitesperpage){
    a logic error :
    the path is made clear using
    corrected parenthesis
    while ((($index, $name, $address, $zip)=$sth->fetchrow_array) && ($sit +eslisted<$sitesperpage)){
    boo::radley
      The second example is clearer, but is effectively the same as the first, since comparison operators have higher precendence than logical operators anyway.

        I'd look to exceptional cases as the problem since the logic looks ok (especially with the added parens).

        What happens if there are no rows returned?

        What happens if a DBI error occurs?

        I'll try to take more detailed look later, but it's been my experience that DBI can throw you some curves that are less than obvious.

Re: Boolean operators and list assignments
by chipmunk (Parson) on Dec 20, 2000 at 22:20 UTC
    I'm actually not sure this is a problem with precedence or with list assignment. Note that the list assignment is in scalar context in both code blocks. The first is in the scalar context of &&; the second is in the scalar context of a while condition.

    However, your two loops have different ending conditions; the first ends when $siteslisted >= $sitesperpage, while the second ends when $siteslisted < $sitesperpage. That may just be an error in retyping the code here, though.

    Based on the code you posted, it's hard to say what the problem is...


    This node was created due to a race condition in the SOPWify feature (I, tye, moved a Categorized Question to Seekers of Perl Wisdom while chipmunk was in the process of composing an answer to it). Please vote for chipmunk's repost of this below so he will get credit for the votes.

    This node will be deleted.

    Originally posted as a Categorized Answer.