Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a tree 1

Status
Not open for further replies.

donny750

Programmer
Jul 13, 2006
145
FR
hello;

It's possible with perl to create a tree like in shell with the mkdir -p command ??

I want to create this folder (this arborescenc)

Client
BA
Custom
Product
Enterprise
History
Product
Price
Quantity

thanks
 
you could create an array with each node you want. and then loop and check/create
Code:
my $path = "dive:/path";
my $tree = "$path/Client,$path/Client/BA,$path/Client/Custom,$path/Client/Custom/Product";
my @tree = split(/,/,$tree);
for(@tree){
  if(!-d $_){
    mkdir $_;
  }
}

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
so i take it you could use the module to specify the full path and any folder within that path that don't exist it creates them?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
How to use mkpath ?
I've seen on cpan but there not example
 
Code:
 use File::Path;
  mkpath "/usr/local/apache/htdocs/articles/2003";

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
You need to get the full path to each path you want to create, then pass them to mkpath:
Code:
use File::Path;

my @paths = qw!/A/test/b /B/test2/a /B/test2/c/H!;

mkpath( \@paths, 0, 0755 );
 
ishnid, you're forever doing something I need explaining - lol , how come your using the bang with the qw ? I understand because of the slash in the paths, so does that mean with qw you can start and end the encapsulation with any character you want?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
You're right about why I'm using it.

I don't think it's quite any character you want, but yes you can do that with all those types of quotes (tr, s, m, qr, qx, q, qq). The main thing to remember is that if you're using brackets, it's the corresponding closing bracket you need, rather than the same character again (which is what you'd normally use). Some examples:
Code:
$string =~ s/foo/bar/;
$string =~ s#foo#bar#;
$string =~ s!foo!bar!;
$string =~ s{foo}{bar};
 
your a genius , thanks for the great info!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Nah, not a genius. Just read "Programming Perl" cover-to-cover on at least 2 occasions.
 
well they do say there is a fine line between madness and genius.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top