Posted By

awnstudio on 11/18/09


Tagged


Versions (?)

Perl Recursive Mkdir


 / Published in: Perl
 

  1. my @dirs = ();
  2. my @path = split /\//, '/foo/bar/baz/baz/bax/cat/dog';
  3. my $file = pop @path;
  4. map { push @dirs, $_; mkdir join('/', @dirs), 0777 } @path;

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: roho on January 27, 2010

I applaud your creativity, however for simplicity's sake the builtin 'mkpath' module can make the entire directory at once, using the directory name extracted by the builtin 'dirname' module. I have found that when I have to go back and maintain what I've written, the simpler I can make it, the better it is for me or some other developer.

1 use File::Path; 2 use File::Basename; 3 my $file = '/foo/bar/baz/baz/bax/cat/dog'; 4 mkpath(dirname($file));

You need to login to post a comment.