in reply to for loops and 'and'

Here's what's happening (I think). The or operator provides scalar context to the first thing it tries (1..10). As the perlop manpage points out, this is false the first time it is tried (I don't really get this...). So for takes 1..11 as its range instead of 1..10.

As for your "real life" issue with the array slice and the string, my tests show that or never gives scalar context to the second operator, and that it always does to the first. Why? I don't know.


UPDATE
Okay, it seems I didn't understand .. before. From perlop:
If either operand of scalar ".." is a constant expression, that operand is implicitly compared to the $. variable, the current line number.
So what happens depends on where it happens. Screwey, eh?

But don't quote me on that



Who is Kayser Söze?

Replies are listed 'Best First'.
Re: Re: for loops and 'and'
by pg (Canon) on Nov 30, 2003 at 16:47 UTC

    No, this demo defeats what you said: (BTW, just for people who does not know, $. is not the line number of your script, but the line input number of last accessed handler. In the demo, the running line number of __DATA__)

    use strict; use warnings; my $s = "1..11"; <DATA> for (1..5); print for (1..10 or $s); <DATA> for (1..7); print for (1..10 or $s); __DATA__ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    This code prints 1..11 twice.

      No, this demo defeats what you said
      I'm not sure which part you are responding to. I assume you understand this code, but for others I will point out that the <DATA> for (1..5) leaves $. set to 5 and then <DATA> for (1..7) makes it 12. For neither 1..10 is it equal to 1, so the flipflop returns false.

        Hm... I don't comment what you said, as I am not sure whether I actually have understood what you said as it meant to be. Any way, try those two pieces, and hope they mean something to you (no harm, food for thought :-):

        use strict; use warnings; my $s = "1..11"; while (my $line = <DATA>) { print "line = $line"; print for (1..10 or $s); print "\n"; } __DATA__ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

        And this:

        use strict; use warnings; my $s = "1..11"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; <DATA>; print for (1..10 or $s); print "\n"; __DATA__ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15