Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks: Is there any perl API that emulates mkdir -p on unix, meaning creates directories recursively. Simply, mkdir $dirname doesn't work. Thanks a lot for your help. Rgds, Tony
  • Comment on Perl API that emulates mkdir -p on unix?

Replies are listed 'Best First'.
Re: Perl API that emulates mkdir -p on unix?
by jZed (Prior) on Jun 13, 2004 at 18:20 UTC
Re: Perl API that emulates mkdir -p on unix?
by wolv (Pilgrim) on Jun 13, 2004 at 18:26 UTC
    Path::Class is also capable of this, if you prefer an OO interface. See its documentation on the $dir->mkpath method.
    use Path::Class qw(dir); dir("foo", "bar")->mkpath;
Re: Perl API that emulates mkdir -p on unix?
by Corion (Patriarch) on Jun 13, 2004 at 18:21 UTC

    There is the File::Path module (in the standard distribution of Perl even), which does exactly that, create intermediate directories if they don't exist.

      However, be aware that File::Path does not play nice with taint. I had to 'roll my own' in-house equivalent because of this.
Re: Perl API that emulates mkdir -p on unix?
by Anonymous Monk on Jun 13, 2004 at 22:25 UTC
    what's wrong with ...
    system( "mkdir -p $dirname" );
      What about platforms that do not have mkdir? Why rely on external tools at all? That just creates a useless dependency that can't be satisfied on all platforms.
Re: Perl API that emulates mkdir -p on unix?
by adamk (Chaplain) on Jun 14, 2004 at 12:52 UTC
    Might be a little heavier that you want, but File::Flat also does this, or rather it lets you just write a file somewhere and it sorts all the details out for you.
    File::Flat->write( '/some/path/that/might/exist.txt', $contents );
    Of course, the neatest thing is that this...
    File::Flat->canWrite( '/some/path/that/might/exist.txt' );
    ... also "means what you think", that is, "Can I write to the file, or create the file, or create as many directories as are needed".

    Great for writing installer-type applications.
Re: Perl API that emulates mkdir -p on unix?
by BrowserUk (Patriarch) on Jun 14, 2004 at 13:47 UTC
    $path =~ s[/][\\]g; system "md $path";
Re: Perl API that emulates mkdir -p on unix?
by Three (Pilgrim) on Jun 14, 2004 at 21:23 UTC

    Here is somthing I wrote to do what you need.

    #Note this is windows centric use strict; make_dir("c\:\\this\\is\\a\\deep\\subdirectory\\"); sub make_dir { my ($inpath) = @_; #Full directory path my $path = ""; #Storage path #Loop through the sections foreach(split/\\/,$inpath) { #Add to path $path .= $_ . "\\"; #Elimnate the drive and check to see if path exists if(!/\:/ and !-e $path) { #If not then make it mkdir $path; #Show status for demo print "Makeing $path\n"; } } }
      Just emulate the Linux function:
      sub MkDirs ($) { my ($Dir) = @_; my @Folders = split(/\\|\//, $Dir); $Dir = shift @Folders; foreach my $Folder (@Folders) { $Dir = File::Spec->catfile( $Dir, $Folder ); mkdir $Dir unless (-e $Dir); } }
        the easiest way with native Perl
        sub mkdirp($) { my $dir = shift; return if (-d $dir); mkdirp(dirname($dir)); mkdir $dir; }