1

Possible Duplicate:
How do I create a directory and parent directories in one Perl command?

Given this scenario: I got an id like "37093". I execute some perl code to create a path name from this

my $id = "37093";
my $path = join('/', split(//, $id ) );

Path is now: "3/7/0/9/3";

Now i want to recursively create all directories that are still not created.

How can this easiliy be done?

Community
  • 1
  • 1
Thariama
  • 50,002
  • 13
  • 138
  • 166

2 Answers2

6

File::Path and Try::Tiny:

use File::Path qw(make_path);
use Try::Tiny;

try {
    make_path '3/7/0/9/3';
} catch {
    warn "make_path failed: $_\n";
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
daxim
  • 39,270
  • 4
  • 65
  • 132
  • 3
    this answer would IMHO be more useful without the non-standard exception handling code. – Alnitak Jun 12 '12 at 09:54
  • i get: "make_path" is not exported by the File::Path module\nCan't continue after... – Thariama Jun 12 '12 at 09:59
  • 2
    @Thariama: you have an old version of `File::Path` (the current version is v2.08). You shoulld upgrade the module, but in this instance you can get away with replacing `make_path` with `mkpath` everywhere. – Borodin Jun 12 '12 at 10:30
  • @Alnitak: what form of exception handling do you consider *standard*? – Borodin Jun 12 '12 at 10:31
  • @Borodin: thanks for the info. i am still not that famililiar with perl. Unfortunately i cannot upgrade the module. – Thariama Jun 12 '12 at 10:35
  • 3
    Either use the legacy interface `mkpath`, or better upgrade the module. You should not run outdated software! [`make_path` already appeared 4 years ago in version 2.06_05](http://search.cpan.org/dist/File-Path/Changes), and [has since been shipped in 39 releases of Perl](http://perlpunks.de/corelist/mversion?module=File%3A%3APath). – daxim Jun 12 '12 at 10:36
  • 1
    "cannot upgrade" surely is hyperbole. You can install your own copy of the module, [local::lib](http://p3rl.org/local::lib) makes this easy; also see [How can I install a CPAN module into a local directory?](http://stackoverflow.com/questions/540640/how-can-i-install-a-cpan-module-into-a-local-directory). – daxim Jun 12 '12 at 10:42
  • 1
    @daxim: i am not allowed to install it on enterprise servers – Thariama Jun 12 '12 at 10:55
  • @Borodin `eval` or using the error handling code built into `make_path` – Alnitak Jun 12 '12 at 11:06
  • 3
    The usual retort is: You are not allowed to install a newer version of an existing core Perl module, but you are allowed to install code you got from Stack Overflow? Where's the difference? What makes code from here trustworthier? – daxim Jun 12 '12 at 11:34
  • @Alnitak: the use of `eval` for trapping and handling exceptions is perhaps classical, but it involves a lot of non-obvious code to use it safely and properly. The `Try::Tiny` module encapsulates all of that arcane code and renders it in an intuitive syntax without enhancing or modifying the functionality of a simple `eval`. Perl has progressed with leaps and bounds since v5.12, and this module represents part of those advances. – Borodin Jun 12 '12 at 11:48
  • @Borodin sure, 100% with you. I just think that _in the context of this answer_ it actually reduces the usefulness of the answer. – Alnitak Jun 12 '12 at 11:58
1
mkdir -p $the_path

may this help.

or write it yourself, but i find this should help and more elegant: http://perldoc.perl.org/File/Path.html

Marcus
  • 6,701
  • 4
  • 19
  • 28
  • how does this have to look like when called from a script? – Thariama Jun 12 '12 at 09:59
  • you could use call system to accomplish it. see http://perldoc.perl.org/functions/system.html – Marcus Jun 12 '12 at 10:04
  • 1
    +1 hmm, works, but is it possible without usage of the system command (doesn't look very elegant) ? – Thariama Jun 12 '12 at 10:09
  • ehh, yes i think you can write a recursive function your self, like this (sorry that i am not a perl coder): (but how to start a new line)..? – Marcus Jun 12 '12 at 10:14
  • @Marcus if you're not a Perl coder, why are you answering this question? – Alnitak Jun 12 '12 at 11:03
  • @Alnitak, i think language is just something that you use to tell the computer how to do, but not something what you have to follow :) – Marcus Jun 12 '12 at 11:05
  • 1
    @Marcus Well FWIW I think this is an utterly lousy answer. Using `system` to accomplish a simple recursive `mkdir` that's implemented in a _standard module supplied with Perl_ is a) inefficient and b) a potential security hole. – Alnitak Jun 12 '12 at 11:08