in reply to A "harmless" alternative to 'map{}'?

Personally, I almost never get bitten by this and it's pretty easy to avoid if you know about it but I came across an oddity myself.
perl -e '@a = map {$_+=1 } (1..3); print "@a\n";' # OK perl -e '@a = map {$_+=1 } (1,2,3); print "@a\n";' # Modification of a + read-only value attempted at -e line 1.
Internally, what's the difference? Why would the first not fail?

-Lee

"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re: Re: A "harmless" alternative to 'map{}'?
by flounder99 (Friar) on Sep 13, 2002 at 15:24 UTC
    I think this has to do with the optimization of the .. operator when fed into a loop. It is handled differently so that things like
    for (1..1000000) {...}
    won't build a million element array before starting the loop.

    --

    flounder