my $filename = 'words'; open(my $myfile, '+>', $filename) or die "Cannot open file '$filename' for writing and reading: $!"; print $myfile @words; seek($myfile, 0, SEEK_SET); srand; rand($.) < 1 and $line = $_ while <$myfile>; close $myfile or die "Cannot close file '$filename': $!";