foreach my $path ( keys %paths ) { #### # : N.B. We explicitly iterate over the paths in order of the # number of directory components. This is so we don't create # directories for a child path before creating directories for a # parent path. # : sort the keys to %path by the number of directory components and # then by a string compare. (both comparisons are ascending.) my @sorted_paths = sort { $a =~ tr/\\// <=> $b =~ tr/\\// || $a cmp $b } keys %paths; # : foreach $path in the @sorted_paths array foreach my $path (@sorted_paths) {