Hacking the Perl core for smarter push and pop

In a recent post, I said I wished that Perl’s built in functions for array containers would work directly on references.

Rather than this (today):

# Assuming $data->{$key1}{$key2} = [ qw/foo bar/ ]
push @{ $data->{$key1}{$key2} }, @stuff;

I wanted this (in the future):

# Assuming $data->{$key1}{$key2} = [ qw/foo bar/ ]
push $data->{$key1}{$key2}, @stuff;

I’ve finished a draft implementation that works for push, pop, shift, unshift and splice. All existing Perl tests pass, as do new tests I’ve written that explore this new functionality. It will even auto-vivify as needed:

my $foo;
push $foo, @stuff; # $foo is now an arrayref

I’m still working on keys, values and each and those look much harder, but I hope to have something to share by next week.

Updated: I added example data to the “given $data…” comments to clarify that it’s an example, not a recommendation about how to initialize a data structure.