http://www.perlmonks.org?node_id=1004804

misterperl has asked for the wisdom of the Perl Monks concerning the following question:

Say I'd like to move the first char in a string to the other end. An obvious choice might be

s/(.)(.+)/$2$1/

But I wondered- what if I tried to move it pairwise along the string (bubble it). It seems like as long as I could get the matching position to move by 1, instead of 2, I could do it that way. Just for academic reasons I wondered..

It seems like something along these lines, with the positive lookhead, could potentially bubble the char:

s/(.)(?=.)/$2$1/g;

which I'd hoped would perform these steps: 12345
21345
23145
23415
23451


but instead it produced 5 warnings and left the string unchanged (I presume the warnings were that $2 was undef)..

Thoughts, wondrous monks?