vr has asked for the wisdom of the Perl Monks concerning the following question:
It's not the issue with order of evaluation, but, looks like, some aliasing is going on, when same variable is used on left and right sides, and modified on the right side.
I was trying to be "clever", slicing array and sliding window at the same time, like this:
>perl -wE "@a=1..9; $i=0; say @a[$i..($i+=3)-1] while $i<@a #1"
No ouput.. Blank lines, but not the output I expected (123\n456\n789\n). Then further:
>perl -wE "$i=1; say for $i..$i++ #2" >perl -wE "$i=1; say for $i..++$i #3" 2 >perl -wE "$i=1; say for do{$i}..do{$i++} #4" >perl -wE "$i=1; say for do{say 'hi!';$i}..do{say 'bye!';$i++} #5" hi! bye! 1 >perl -wE "$i=1; say for do{$i?$i:($i+1)}..$i++ #6" >perl -wE "$i=1; say for do{$i?$i:($i++)}..$i++ #7" 1
Maybe I'm wrong and it's not a bug, and there's some logical explanation? Last examples are frightening.
Edit. Corrected the output I expected (thanks, Marshall). Added line numbers to one-liners to refer to them.
Edit 2. But what about the do returning either value (#5, #7) or alias (#4, #6)?
do:
do BLOCK ...Returns the value of the last command in the sequence of commands indicated by BLOCK.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strange aliasing(?) when modifying variable in range operator
by LanX (Saint) on Jun 09, 2018 at 17:28 UTC | |
by vr (Curate) on Jun 09, 2018 at 18:01 UTC | |
by LanX (Saint) on Jun 09, 2018 at 18:19 UTC | |
|
Re: Strange aliasing(?) when modifying variable in range operator
by Marshall (Canon) on Jun 09, 2018 at 17:15 UTC | |
|
Re: Strange aliasing(?) when modifying variable in range operator
by ikegami (Patriarch) on Jun 11, 2018 at 07:24 UTC | |
|
Re: Strange aliasing(?) when modifying variable in range operator
by LanX (Saint) on Jun 09, 2018 at 18:39 UTC |