opendir(MYDIR, $dirpathname) or die "Cannot open directory: $!"; foreach my $xmlfile (grep { -f $_ and /[.]xml$/ } readdir MYDIR) { do_stuff_with($xmlfile); } closedir MYDIR;