Peter Stuifzand

Reversing an arrayref is too hard

Let’s say I have this line of code

my $numbers = [ 1, 2, 3, 4, 5, 6 ];

and I want to reverse the items in the list. In Perl 5 there is this useful builtin function which does just that. There problem is, it only works on lists, not arrayrefs. If you use it on an arrayref, you will reverse the string representation of the arrayref. Which will return something like this:


This is not very useful. I think, the only way to reverse a arrayref is by dereferencing it to an array, then reverse it, and then make it an arrayref again. Something like this:

my $newarray = [ reverse @$numbers ];

This line contains synthetic code. It should be easier to reverse arrays.

© 2023 Peter Stuifzand