srand; # seed the random generator open(FILE,"file.txt"); # put the file contents into one big scalar while() { $file .= ; } close(FILE); $file =~ s/\r/ /g; # remove carriage returns and replace them with spaces $file =~ s/\s+/\s/g; # remove multiple consecutive spaces @file = qw($file); # seperate the file into single words $word = $file[rand($#file)]; # select a random word