Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Odd splice behavior between perl/OS versions (undefined)

by tye (Sage)
on Oct 28, 2016 at 19:14 UTC ( [id://1174913] : note . print w/replies, xml ) Need Help??

in reply to Odd splice behavior between perl/OS versions

The behavior depends on the order in which different parts of the operation are done. If this were C code, then it would be labeled "undefined behavior". Since it is Perl code, the strict meaning of that phrase from the C standard doesn't quite apply. But it is the type of behavior that is the most likely to end up changing when mundane maintenance is done on Perl's source code, including optimizations.

I believe the crux of the difference is whether you get the value of $#stuff passed to the outer instance of splice from before or after the inner instance of splice has removed $stuff[3]. Though I can also imagine more complex ways that a simple optimization, bug fix, or just reworking of Perl's code could cause your code to behave differently.

Avoid changing @stuff in multiple ways in a single expression and using values like $#stuff from within an expression where that value is changed.

- tye