opendir(DIR, "$dir") || die "cant open dir.\n"; while (defined(my $file = readdir DIR) { do something; } closedir(DIR);