in reply to recursive mkdir

I'm not suggesting you continue writing the sub yourself, but here's a way (probably not the best way) to write the solution without using recursion (which should be saved for when really needed).
sub my_mkdir { my $path = shift; my $perms = shift; my @parts = split /\//, $path; for my $num (1..$#parts) { my $check = join('/', @parts[0..$num]); unless (-d $check) { mkdir( $check, $perms ); } } }

Replies are listed 'Best First'.
Re: Re: recursive mkdir
by eg (Friar) on Jan 08, 2001 at 14:37 UTC

    Neat. You can get rid of the join by using a .=.

    sub my_mkdir { my ($path, $perms) = @_; my $dir = '.'; for ( split /\//, $path ) { mkdir $dir .= "/$_", $perms; } }
Re: Re: recursive mkdir
by chipmunk (Parson) on Jan 08, 2001 at 10:59 UTC
    That's a nice iterative solution. One small correction: the for loop should start at 0, rather than 1, because it may need to create the very first directory in the path.

    (I tested it with my_mkdir('dir1/dir2/dir3', '0777'); it worked only if dir1 already existed.)