#!/usr/bin/perl -w use strict; use List::Util qw(shuffle); print map { !$#$_ ? $_->[0].' ' : join '', $_->[0], shuffle(@{$_}[1 .. $#$_-1]), $_->[$#$_], ' ' } map { [ split // ] } @ARGV ;