Fix disappearing $_ inside callbacks. #47
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
I ran into a bug with callbacks where assigning $_ to another variable causes it to become undef, preventing the method from executing. I've added a test case to t/70callbacks.t in this pull request that illustrates the problem.
This happens because sv_2mortal sets the SvTEMP flag on the new defsv, enabling another variable to "steal" its string buffer (rather than make a copy, since the original is normally about to be discarded anyway).
This patch switches off the SvTEMP flag on the new sv after mortalizing it. This doesn't introduce a memory leak because it still exists on the temps stack, and it prevents the buffer theft.
List::Util had a similar issue and dealt with it in the same manner:
[1] https://rt.cpan.org/Public/Bug/Display.html?id=96343
[2] Dual-Life/Scalar-List-Utils@1bddb84
[3] Dual-Life/Scalar-List-Utils@0925609
Thanks!