Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Mysterious for behavior

by hardburn (Abbot)
on Apr 21, 2004 at 19:08 UTC ( #347098=note: print w/replies, xml ) Need Help??


in reply to Mysterious for behavior

I belive what is happening is that each statement in the for is being evaluated from left to right, thus making the $i = 1 portion win out. The list the loop works on becomes ($i, $i, $i), which makes the complete code equivilent to:

my $i = 1; for ( $i, $i, $i ) { print "$_ and $i\n"; }

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Mysterious for behavior
by Roy Johnson (Monsignor) on Apr 21, 2004 at 19:12 UTC
    Thanks!

    Update 2: Because of the explanations given, I understand the phenomenon, but I think it can be explained more explicitly than has been done, so I'm going to give it a shot:

    First of all, all the expressions will be evaluated when the list is built. That is, before any iteration happens, all of those assignments have been executed.

    The next thing to understand is that lists may include ordinary values and lvalues; an assignment yields an lvalue, though an expression of which an assignment is just part (e.g., 0+($x=3)) is an ordinary value. What is stored in an lvalue can change even after the lvalue is inserted into the list, so its value depends on when you look at it.

    The aliasing of a for loop is not fundamental to this. Simply assigning the same list to an array demonstrates the same phenomenon:

    my $x; my @a = ($x=1, $x+=1, $x-=5); print "@a\n"; #yields: #-3 -3 -3
    One final interesting note: postincrement does not yield an lvalue, but preincrement does.

    The PerlMonk tr/// Advocate

      Post-increment does not yield a new lvalue, but it does have an lvalue, and does modify the variable. Update: I don't what I was thinking about there. Post-increment has an rvalue. (That's what happens when you try to post a message just before quitting time!) However, below is still semi-interesting. Consider:

      print "$_ and $i\n" for ($i = 1, $i++, $i++); __OUTPUT__ 3 and 3 1 and 3 2 and 3

      The $i is being modified, and the value before the post-increment is used in the list. Playing around, you could do something like:

      print "$_ and $i\n" for (\($i = 1, $i++, $i++)); print \$i , "\n"; __OUTPUT__ SCALAR(0x8176c44) and 3 SCALAR(0x815dcc0) and 3 SCALAR(0x815dad4) and 3 SCALAR(0x8176c44)
      This shows that the first element of the list is the $i variable, and that perl generates two temporary scalars to hold the values of $i prior to increment. Also, the entire list is evaluated before iteration.

      Interesting stuff you found here!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347098]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-10-24 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (286 votes). Check out past polls.

    Notices?