How to create sub-directory and main directory.

I want to create a directory and a sub directory in it!

I was using: mkdir($dir,0777);

This will work for the last sub directory name.
if $dir="/home/shop/test/one";
the code will create a new directory in dir test if the dir tes already exists!

But what if the directory test does not exists?
What code do I have to use to check if dir home is available - if not create - if shop is available - if not create - if shop is available - if not create - and at last if one is available - if not create!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

use File::Path;

mkpath( $dir, 0777);
Ooops. Let's correct that:

use File::Path;

mkpath( $dir, 0, 0777);

The documentation can be viewed at:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weversbvAuthor Commented:
I file::path a standard module/ function in perl?
If not can it be done with standard modules/ functions?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Yes, File::Path should be a standard part of your Perl distribution, if it's recent enough. I have it on 5.6.0.

Yes, you can do it with plain Perl, but that would require breaking up the pathname into components, making each directory if it doesn't already exist, and fixing the modes on the directories after you've created their subdirectories. In short, most of the code in the File::Path module.

On a Unix or Linux system, you could use

system( "mkdir -p $dir");

if the default permissions are acceptable.
weversbvAuthor Commented:
I use:

use File::Path;
mkpath( $dir, 0, 0777);

But the rights will be set to 0755?
What is wrong?
#you have umask set to 022, try
umask 0;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.