... my $dirpath=... ... while (defined (my $file = ...) ) { ... foreach my $filename (...) { ... } ...