Someone asked me today about Perl array operations and it turned out he didn't know that all basic array operations, such as push, pop, shift, unshift can be implemented with just splice. So I thought I'd write a blog post that shows how awesome splice is.

splice

Splice has the following prototype:

splice(array, [offset, [length, [list]]])

Splice replaces length elements in array at offset with list. Offset, length, and list can be omitted, which makes it a really powerful function that can do a bunch of array operations.

push via splice

To push to an array via splice, you need to set offset to the end of the array, length to anything, and list to the list of values that you want to add.