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

If I want to make a directory called '/usr/local/foo/bar/baz', and I don't know if '/usr/local/foo/bar' or '/usr/local/foo' exists, is there a simple recursive mkdir command that I'm just failing to find, or do I have to roll my own? Or is there some reason that a recursive mkdir is a bad idea?

Thanks!
-Pileofrogs

Replies are listed 'Best First'.
Re: Dumb recursive mkdir question...
by Corion (Patriarch) on Oct 22, 2007 at 21:09 UTC
Re: Dumb recursive mkdir question...
by moritz (Cardinal) on Oct 22, 2007 at 21:09 UTC
Re: Dumb recursive mkdir question...
by tye (Sage) on Oct 22, 2007 at 21:38 UTC

    I can't believe that nobody recommended File::Path:

    use File::Path qw( mkpath ); mkpath( "/usr/local/foo/bar" ); # die()s on error

    - tye        

      mkpath( "/usr/local/foo/bar" ); # die()s on error

      With the new version of File::Path, you can stop things from dieing quite nicely:

      mkpath("/usr/local/foo/bar", {error => \my $err});

      This new version is bundled with 5.10, and will probably wind up in 5.8.9 as well.

      • another intruder with the mooring in the heart of the Perl

      I'm almost with gamache on this one, if it's not something you need to do often in the program, or if you don't have the access/ability to install the File:Path module, then doing 'mkdir -p' in a "perlish" way might be even easier (considering you're in a Unix environment):
      $path = "/path/to/real/deep/folder"; $out = `mkdir -p $path 2>&1`; die "Can't make path: $out\n" if ($? > 0);
Re: Dumb recursive mkdir question...
by andyford (Curate) on Oct 22, 2007 at 21:11 UTC
Re: Dumb recursive mkdir question...
by Sidhekin (Priest) on Oct 22, 2007 at 21:11 UTC

    You are likely looking for File::Path::mkpath.

    ... which does not look like a bad idea at all to me. :)

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Dumb recursive mkdir question...
by gamache (Friar) on Oct 22, 2007 at 21:19 UTC