Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

"mkdir -p" equivalent?

by ccarden (Monk)
on Aug 05, 2003 at 19:01 UTC ( #281122=perlquestion: print w/replies, xml ) Need Help??
ccarden has asked for the wisdom of the Perl Monks concerning the following question:


Is there a way, with the perl mkdir command, to make parent directories as needed?

At least up to 5.6x, perl does not seem to have an equivalent to the -p switch in the *nix version.

If perl's mkdir won't handle it, can anyone suggest a simple workaround?




Replies are listed 'Best First'.
Re: "mkdir -p" equivalent?
by esh (Pilgrim) on Aug 05, 2003 at 19:06 UTC
    Check out mkpath in File::Path
Re: "mkdir -p" equivalent?
by blue_cowdawg (Monsignor) on Aug 05, 2003 at 19:27 UTC

    As msh said somewhat tersely check out File::Path and read the doco.

    Here is a sample of its use:

        #!/usr/bin/perl -w ############################################## use strict; use diagnostics; use warnings; use File::Path; mkpath(["/usr/tmp/over/the/rainbow"],1,0755);

    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

Re: "mkdir -p" equivalent?
by skx (Parson) on Aug 06, 2003 at 11:35 UTC
Re: "mkdir -p" equivalent?
by Abigail-II (Bishop) on Aug 05, 2003 at 21:45 UTC
    In such cases, I tend to use:
    system mkdir => '-p' => @dirs;

    After all, that's why we have a Unix toolkit - it's not a shame to use it.

    And yeah, I know that doesn't work on Windows. But I said that I tend to use it, and I don't do Windows. ;-). (But if I do, I install cygwin, and I've a Unix toolkit).


      Ahh, but here is the real problem: certain customers forbid us from changing the platform load. Has anyone else run into this? It's very common with many of the projects that I have worked on in the Detroit area.

      Security and system administration are very, very tight at the auto companies. Everything must be served, and if I want to use cygwin, I'd have to jump through hoops to serve it along with the desired version of perl. Btw, perl is frowned upon quite seriously at the autos. The patent response from certain admins is: "Who's going to support it? If it breaks, who do I call, the Internet? Ha. Ha. Ha."

      Geez, Louise.

      So, for now, the answer is that we keep one server at the back end of our system that runs all of our perl scripts. This allows us to use perl without running into too many complaints. But the server could easily change from *nix to win32 in a year. So my code needs to be able to run if such a change occurs.

      Thanks to everyone for the valuable input.

      -- ccarden

        And did you tell them you can buy commercial support for Perl?

        Also, ask them in which language they like there server-side programs to be run in? Python and PHP suffer the same "problems" as Perl. If it's written in C, changing from Unix to Windows won't be trivial. Which leaves Java?


Re: "mkdir -p" equivalent?
by jhasting (Initiate) on Aug 05, 2003 at 21:16 UTC
    For a super simple suggestion: could you use the OS mkdir command via system or ``
    `mkdir -p a/b/c`;
      That's how the code currently reads, but I need it to be portable.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://281122]
Approved by blue_cowdawg
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2018-06-22 15:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.