my $dirname = '/usr/bin';
chdir($dirname) or die("can't chdir $dirname: $!\n");
opendir(DIR, $dirname) or die("can't opendir $dirname: $!\n");
while (defined(my $fn = readdir(DIR))) {
next unless (-f $fn);
#...etc...
}
closedir(DIR)