my @files=$ftp->ls or $error=1; $ftp->quit if $error; foreach(@files) { if (/^yourRegex/i) #Filter file names { print "$_\n"; unshift @match, $_; } } defined $match[0] ? $fname = $match[0] : $ftp->quit;