... my $pw; do { $pw = make_new_pw(); } while ( grep { $pw eq $_ } @stored ); ... # update: moved the declaration of @chars into the subroutine, # where it belongs: sub make_new_pw { my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw( ! @ $ % ^ & *) ); my $newpw = ""; for ( 1 .. 17 ) { $newpw .= $chars[ rand @chars ]; } return $newpw; }