I was recently working with Perl's splice function and ran into an anomaly. In this case I was testing on Windows via WAMP running ActiveState Perl ver. v5.16.3. Everything looked good but when I ran the same code on one of our Linux severs (RedHat), this one running Perl ver. v5.10.1 (yes, it's old and someday it will no longer be), I got a different result which really surprised me. Here is the code in question and the different outputs:

#!/usr/local/bin/perl use strict; use warnings; my @stuff = qw(one two three four five six seven eight nine ten); print "Before:\n\n", join(",", @stuff), "\n"; splice(@stuff, $#stuff, 0, splice(@stuff, 3, 1)); splice(@stuff, $#stuff, 0, splice(@stuff, 3, 1)); splice(@stuff, $#stuff, 0, splice(@stuff, 3, 1)); print "\nAfter:\n\n", join(",", @stuff), "\n"; exit 1;

On my Windows 10 workstation with Perl v5.16.3 I get what I expect:

Before: one,two,three,four,five,six,seven,eight,nine,ten After: one,two,three,seven,eight,nine,ten,four,five,six

But on the Linux box with Perl v5.10.1 I get something else:

Before: one,two,three,four,five,six,seven,eight,nine,ten After: one,two,three,seven,eight,nine,four,five,six,ten

Our other Linux server (Ubuntu) is running Perl ver. v5.14.2 and the test script's output on that box is the same as my Windows 10/WAMP output so it would seem to be, at least in part, a Perl version issue.

I searched here and Googled around and was surprised not find anything about this. The only reference to a change in splice's behavior that I found is regarding allowing it to accept an array ref for its EXPR argument starting with 5.14. That's considered experimental behavior and doesn't apply here since I've chosen not to use refs.

Any idea why the change in behavior? Bug? Thanks for any insight you can offer.

-Simon

In reply to Odd splice behavior between perl/OS versions by SimonSaysCake

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.