Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Wrong order of evaluation in map blocks #280

Open
rurban opened this issue May 12, 2017 · 0 comments
Open

Wrong order of evaluation in map blocks #280

rurban opened this issue May 12, 2017 · 0 comments
Assignees
Labels
Milestone

Comments

@rurban
Copy link
Member

rurban commented May 12, 2017

The key should be stored before the value is calculated. Left to order evaluation of block members (i.e. a list), i.e. copy away GvSV(defgv).

perl -e'sub x{$_="ouch"};%h=map{$_=>x}(0..3);print join" ",%h' => (ouch=>ouch), but should be
(1=>ouch, 2=>ouch, 0=>ouch, 3=>ouch), because the map list is (ouch, ouch, ouch, ouch, ouch, ouch), which is then collapsed to (ouch=>ouch).
Same problem with sub x{$_="ouch"};@h=map{$_=>x}(0..2);print join ", ",@h => (ouch, ouch, ouch, ouch, ouch, ouch) which should be (0, ouch, 1, ouch, 2, ouch) instead.

$_=>x => x changes $_, but should not influence the former key/list result.

See PatrickCronin/Map-Functional#1

Looks like the problem cannot be solved at runtime.
mapstart sets a fresh $_, then the block produces the stack entries, then mapwhile consumes them.
The problem happens in the block, which overwrites $_ before mapwhile can consume them.
The compiler could be changed to split the block into static list entries with interspersed mapwhile
consumers.

mapstart { stmt, stmt } mapwhile =>
mapstart { stmt; mapwhile; stmt } mapwhile

This would help for the obvious static lists {$_=>$v} and {$_=>x}, but not for the 2nd problem with the call producing a list {x}. Which is fine.


For hash assignments we could restrict the mapwhile producer to accept only pairs, not arbitrary lists. Something like use strict 'hashpairs'. See #281

@rurban rurban self-assigned this May 12, 2017
@rurban rurban added the bug label May 12, 2017
rurban added a commit that referenced this issue May 13, 2017
warning, when a mapblock assigned to a hash produces no pairs.
See #280

TODO: warn only once, at first mapwhile violation, not for every iteration.
@rurban rurban changed the title Wrong order of evaluation on map blocks Wrong order of evaluation in map blocks May 13, 2017
@rurban rurban modified the milestones: v5.26.1c, v5.28.0c May 14, 2017
rurban added a commit that referenced this issue May 14, 2017
This warning is only printed once at the very first map iteration.
See #280
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant