You can sort by putting folders first and then sorting by file/dir name,
# $src pointing to folder open with opendir
my @sorted_dir =
map $_->[0],
sort {
$a->[1] <=> $b->[1]
||
$a->[0] cmp $b->[0]
}
map [ $_, -f "$src/$_" ],
readdir($DIR);
While similar effect can be achieved with,
for my $file (sort { -f "$src/$a" <=> -f "$src/$b" } readdir($DIR)) {
print "$file\n";
}
it's slower and inefficient as it more often goes to file system checking if directory entry is a plain file.