in reply to Re: infinite loop on while (@array)
in thread infinite loop on while (@array)

If you need a real infinite loop, then this one could suit you better:

Or the expressionless C-style loop. (The while(1) loop will be converted into it to optimize, so why not type it yourself?) They're a nice visual way to say: this is infinite, while while(1) and while(0) don't differ a lot.

for (;;) { # ... }

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Replies are listed 'Best First'.
Re: Re: Re: infinite loop on while (@array)
by demerphq (Chancellor) on Mar 27, 2002 at 10:43 UTC
    <pedant class="minor">

      The while(1) loop will be converted into it to optimize

      Im fairly sure that if you look into the documentation again that the c style for loop is actually converted into a while() block. Not the other way round.

      Hmm, just doublechecked perlsyn and it seems that they arent clear, nor is the camel, as to whether these are actually handled internally as forms of each other. However the camel has the following interesting addition to the perlsyn:

      The for loop can be defined in terms of the corresponding while loop.

      Thus, the following:

      for ($i = 1; $i < 10; $i++) { ... }
      is the same as:

      $i = 1; while ($i < 10) { ... } continue { $i++; }
      (Defining the for loop in terms of a continue block allows us to preserve the correct semantics even when the loop is continued via a next statement. This is unlike C, in which there is no way to write the exact equivalent of a continued for loop without chicanery.)
      <super>emphasis added by me</super>
    </pedant>

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      Using B::Deparse under ActivePerl b629 (aka 5.6.1)...

      C:\> perl -MO=Deparse -e "while (1) { print 1; }" for (;;) { print 1; } -e syntax OK C:\> perl -MO=Deparse -e "for (;;) { print 1; }" for (;;) { print 1; } -e syntax OK

      But if I try the for example that demerphq quotes under Perl 5.5.3 (on FreeBSD), I get ...

      $ perl -MO=Deparse -le 'for ( my $i = 0; $i < 10; $i++ ) { print 1 }' -e syntax OK my $i = 0; while ($i < 10) { print 1 } continue { ++$i }

      Hmmm...

          --k.


        Actually this means nothing. (Sorry ;-) The issue is still open...
        D:\Development> perl -MO=Deparse,-x3 -e "for ($i = 0; $i < 10; ++$i) { +print $i;}" $i = 0; while ($i < 10) { print $i; } continue { ++$i } -e syntax OK
        From B::Deparses documentation <super>(Emphasis by me)</super>:
        -xLEVEL
        Expand conventional syntax constructions into equivalent ones that expose their internal operation. LEVEL should be a digit, with higher values meaning more expansion. As with -q, this actually involves turning off special cases in B::Deparse's normal operations. If LEVEL is at least 3, for loops will be translated into equivalent while loops with continue blocks; for instance
        for ($i = 0; $i < 10; ++$i) { print $i; }
        turns into
        $i = 0; while ($i < 10) { print $i; } continue { ++$i }
        Note that in a few cases this translation can't be perfectly carried back into the source code -- if the loop's initializer declares a my variable, for instance, it won't have the correct scope outside of the loop.
        In fact if anything I would say that Deparse suggests that in fact for(;;) is converted into while{}continue{} not the other way around. Although the trailing paragraph that I quoted suggests that its not quite so straightforward as perhaps we might like...

        :-)

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

        But if I try the for example that demerphq quotes under Perl 5.5.3 (on FreeBSD), I get ...

        for (;;) ne for (EXPR; EXPR; EXPR). The expressionless C-style for is special: it's an infinite loop. while (1) would be very inefficient, because it would have to check an expression on every iteration. Perl is smart, and knows what you're doing, and optimizes as follows:

        2;0 juerd@ouranos:/root$ perl -MO=Deparse -e'while (1) { print }' for (;;) { print $_; } -e syntax OK
        You see? it's converted to for (;;) because it's more effecient. We can't benchmark the difference because of this optimization...

        U28geW91IGNhbiBhbGwgcm90MTMgY
        W5kIHBhY2soKS4gQnV0IGRvIHlvdS
        ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
        geW91IHNlZSBpdD8gIC0tIEp1ZXJk