in reply to Auto-incrementing strings in for loops

Jade,
it seems to me that for part 2 you are 'say'ing the for loop and thus $_ will be the constant 1 then 2.
For part three perhaps perl is compiling 'for 1' to be just '1 ' and thus you are attempting to $_++ a constant ('1')?
  • Comment on Re: Auto-incrementing strings in for loops

Replies are listed 'Best First'.
Re^2: Auto-incrementing strings in for loops
by ikegami (Patriarch) on Nov 20, 2009 at 04:47 UTC
    No. The for has the lowest of priorities.
    $_++ and say for 1..2;
    is not equivalent to
    $_++ and ( say for 1..2 );

    That's not even legal. The original statement is indeed equivalent to

    ( $_++ and say ) for 1..2;
Re^2: Auto-incrementing strings in for loops
by JadeNB (Chaplain) on Nov 20, 2009 at 04:19 UTC
    it seems to me that for part 2 you are 'say'ing the for loop and thus $_ will be the constant 1 then 2.
    Thanks for your thoughts, but the parsing you suggest is not quite right:
    $ perl5.10 -MO=Deparse,-p -E 'say for 1..2' BEGIN { $^H{'feature_say'} = q(1); $^H{'feature_state'} = q(1); $^H{'feature_switch'} = q(1); } ; say($_) foreach (1 .. 2); -e syntax OK
    That is, say for ... is understood as say($_) for .... The problem is, unfortunately, much sillier than that: for aliases $_, so I wasn't incrementing the variable that I thought I was incrementing!