reverse
reverse LISTIn list context, this function returns a list value consisting of
the elements of LIST in the opposite order.
The function can be used to create descending sequences:
for (reverse 1 .. 10) { ... }Because of the way hashes flatten into lists when passed as a
LIST, reverse can also be used to invert a hash,
presuming the values are unique:
%barfoo = reverse %foobar;
In scalar context, the function concatenates all the elements of
LIST and then returns the reverse of that
resulting string, character by character. By character we mean
codepoint, not grapheme. That means you will inappropriately reverse the
pieces of “\r\n” into “\n\r”, causing combining characters to
accidentally apply to the wrong base character. To do a reverse on
graphemes instead of by codepoint, do this:
$codeuni = join "" => reverse $unicode =~ /\X/g;
A small hint: reversing a list sorted earlier by a user-defined function can often be achieved more easily by sorting the list in the opposite direction in the first place.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access