Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Tuesday July 20, 2004
12:15 PM

mkdir -p in Perl

[ #19954 ]

The equivalent of mkdir -p in Perl is:

use File::Path qw(mkpath);
mkpath $directory;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • On that note, I submit that File::Path is misnamed. The summary of the module is given as "create or remove directory trees." In other words, it's mkdir -p and rm -rf in Perl.

    Convenient to have, hard to find.

    --
    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • It is a very useful module. However, the naming is a bit screwy. If it's called File::Path, why does it have mkpath() and rmtree(). Surely it should be rmpath() in that case?

      Anyway, it's another handy core module that people don't notice because of the name, sigh.

  • You left off a bit:
    ... or die "mkpath: Boom!";
    • Actually you most often don't care whether you could create the directory structure, you care whether it's there after the call. So it's:

      mkpath($directory);
      -d $directory or die("Could not create ($directory)");

      Conversely, if you actually want to create the directory, often the real intent is to make sure it's newly created. So delete it first, then create it.

      rmtree($directory);
      -d $directory and die("Could not remove ($directory)");
       
      mkpath($directory);
      -d $directory or die("Could not create ($direct

      • In the case of rmtree, perhaps the test should be -e, not -d. I believe your test would fail if the file existed and was not a directory. (Could be wrong.)

        --
        J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • The reason I did not do this is it doesn't work. mkpath returns a list of the directories created, not a success or failure. And if you run the program a second time, you get back an empty list (false) because the directory is already created.

      I like jplindstrom's approach, although in my case I immediately execute a chdir or die, which has more or less the same effect.

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers