in reply to pop and shift not reversible

I believe you will find that pop and shift are reversible, but your code is not. Specifically, if you reverse the data in your toy arrays, it will be pop that processes the entire array, and shift that stops early.

Expanding on what has been said previously, you seem to be coding in the belief that while ( my $next = pop @x ) { ... } stops when the array is empty. In fact, it stops when it encounters the first false value. In Perl, "false" means undef, 0, or ''.

This is why the correct loop is while ( @x ) { my $next = pop @x; ... } (or shift, as the case may be.)