in reply to directory listing

#!/usr/bin/perl use strict; use warnings; my $dir = '/opt/DSPKG'; opendir my $dh, $dir || die "Can't open $dir: $!\n"; open my $out, '>', '/tmp/pkgtest' || die "Can't open pkgtest: $!\n"; foreach ( readdir $dh ) { print $out $_ . "\n" if -d "$dir/$_" && $_ !~ /^\.{1,2}$/; } close $out || die "Error closing pkgtest: $!\n";

Notice the following, which will help you in the long run:

If building the array with grep and handing it to foreach is clearer for you, it's fine to do things that way. I wouldn't want to promote doing away with that simply for the sake of doing so. I, personally, find it clearer when the test is made inside the main loop rather than in a secondary looping construct.

I like doing away with the array as well, as it lets me focus on what's being done to the data rather than where it's being stored.

Scalar filehandles and directory handles are how things tend to be done these days. The language is moving away from barewords for such things, and for scoping reasons it's smart to use scalars. Bareword handles are package-scoped. Scalar ones can be declared lexical with my.