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

Dumb recursive mkdir question...

by pileofrogs (Priest)
on Oct 22, 2007 at 21:06 UTC ( [id://646577]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://646577]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-29 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found