Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this is somebody's gadget code. It doesn't look like the way you would want to write anything. But what it might illustrate best is that (in at least some versions) Perl is really running a more basic for loop, where:</p> <pre><code>for ( @l ) { #... } </code></pre> <p>Is replaced by: </p> <pre><code>for ( my $i = 0; $i &lt; @l; $i++ ) { local $_ = $l[$i]; #... } </code></pre> <p>Thus, because <code>@l</code> is <code>( 'c' )</code> when we've gone through twice, our trips through is already greater than <code>scalar( @l )</code>, so we're out. I've tested it out in a number of cases, and they seem to be equivalent. </p> <p>Below is the code I wrote to test cases. From it we can see that because of the shift, as soon as we're halfway through, the loop will exit. </p> <pre><code>use strict; use warnings; use English qw&lt;$LIST_SEPARATOR&gt;; use Test::More 'no_plan'; sub test_loops_without_shifts { my @l = @_; my @tests; for ( @l ) { push @tests, $_; } my @l2 = @_; my $n = @tests; my $i = 0; for ( $i = 0; $i &lt; @l2; $i++ ) { local $_ = $l2[$i]; my $x = shift @tests; my $g = $_; is( $g, $x, "expected: $x, got: $g" ); } is( $n, $i ); is_deeply( \@l, \@l2, do { local $LIST_SEPARATOR = .', '; "leftover: ( @l ) = ( @l2 )" } ); return $i; } sub test_loops { my @l = @_; my @tests; for ( @l ) { push @tests, shift @l; } my @l2 = @_; my $n = @tests; my $i = 0; for ( $i = 0; $i &lt; @l2; $i++ ) { local $_ = $l2[$i]; my $x = shift @tests; my $g = shift @l2; is( $g, $x, "expected: $x, got: $g" ); } is( $n, $i ); is_deeply( \@l, \@l2, do { local $LIST_SEPARATOR = ', 'c; "leftover: ( @l ) = ( @l2 )" } ); return $i; } is( test_loops( 'a'..'c' ), 2 ); is( test_loops( 'a'..'d' ), 2 ); is( test_loops( 'a'..'e' ), 3 ); is( test_loops( 'a'..'f' ), 3 ); is( test_loops( 'a'..'g' ), 4 ); is( test_loops_without_shifts( 'a'..'c' ), 3 ); is( test_loops_without_shifts( 'a'..'d' ), 4 ); is( test_loops_without_shifts( 'a'..'e' ), 5 ); is( test_loops_without_shifts( 'a'..'f' ), 6 ); is( test_loops_without_shifts( 'a'..'g' ), 7 ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload