@moves = qw(R U R' U' R' F R2 U' R' U' R U R' F');
$state = "UF UR UB UL DF DR DB DL FR FL BR BL UFR URB UBL ULF DRF DFL DLB DBR";

sub move {
  my ($pos) = @_;
  eval "\$pos =~ y/$from/$to/";
  $pos;
}

print "Moves: @moves\nStart: $state\n";
for $move (@moves) {
  $move =~ s/'/3/;
  ($side, $angle) = split //, $move . 1;
  $from = $to = '';
  for (split ' ', $state) {
    if (/.../ && /$side/) {
      ($t, $f) = split '', "$'$`";
      $from .= $f;
      $to .= $t;
    }
  }
  $state =~ s/\w*$side\w*/move($&)/ge while $angle--;
}
print "End:   $state\n";