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

Monks,

why is

for ( ; ; my @socks = $BASKET -> can_read(2) ) { ... } # different than: while ($BASKET) { my @socks = $BASKET -> can_read(2); ... }
other than the obvious conditional difference. i found that my IO::Select code was not working with the first example, but when I switched to the second example, it worked. Anyone have an idea as to what is causing this?

thanks,
brother dep.

--
transcending "coolness" is what makes us cool.

Replies are listed 'Best First'.
Re: for vs. while question
by MrNobo1024 (Hermit) on Mar 07, 2001 at 05:49 UTC
    in for(;;code), the code is executed at the end of the loop, not the beginning.
Re: for vs. while question
by merlyn (Sage) on Mar 07, 2001 at 05:49 UTC
    Move your expression from third position to the first position:
    for (my @socks = $BASKET -> can_read(2); ; ) { ... }
    In the place you put it, it's executed at the bottom of the loop, not the top.

    -- Randal L. Schwartz, Perl hacker


    update: No, read further down.
      Won't that put it outside of the loop (so it's only executed once)?
        Blimey yes. Ugh. OK, make it the conditional then:
        for (;$BASKET and (my @socks = $BASKET -> can_read(2)) or 1); ) { ... }
        and now it's nearly like the original non-for code.

        Probably simpler just to use the while. {grin}

        -- Randal L. Schwartz, Perl hacker