use Cwd; sub process_files { my $path = shift; my $dir = getcwd; chdir( $path ) or die "chdir($path) failed: $!"; opendir (my $dh, ".") or die "Unable to open ".": $!\n"; my @files = grep { !/^\.\.?$/ } readdir ($dh); closedir ($dh); foreach(@files){ if ( -d $_ ) { process_files($_); } elsif ( $_ =~ /\.pdw$/ ) { push @files_erg, $_; } } chdir ( $dir ) or die "chdir($dir) failed: $!\n"; }