Nicely written, but you're having it return a ref in scalar context, whereas the OP wanted it to behave like a list (which returns the last element). Change the last $list to $list->[-1]
Caution: Contents may have been coded under pressure.
Comment on Re^2: inline caching a list-generating sub