in reply to How do I recursively create a directory tree/filesystem?

For a more simple solution, you may want to look at the mkpath function exported from the File::Path module that is part of the core installation. Alternatively, if you are looking at writing your own function for educational purposes, you might want to look at this thread where I contributed (based on an earlier post from mirod) a more portable solution using File::Spec using a loop over the path components rather than employing recursion (Recursion makes it very easy to write bad code - Whenever I see recursive code my first urge is to refactor immediately :-).

The code ...

sub create_dirs { use File::Spec; my $file = shift; my $dir; foreach ( File::Spec->splitdir( (File::Spec->splitpath( $file ))[1 +] ) ) { $dir = File::Spec->catdir($dir, $_); next if -d $dir; mkdir( $dir ) || die $!; } }
 

Replies are listed 'Best First'.
Re: Answer: How do I recursively create a directory tree/filesystem?
by Ionitor (Scribe) on Jul 22, 2002 at 13:14 UTC
    That's not exactly what the program was intended to do. It appears to attempt to create a directory tree, where each directory contains two more directories (a and b) to a depth of three directories. mkpath would work, but it would have to be fed a list of directories that would probably most easily be generated by recursion.