Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Recursive directories

by rustic (Monk)
on Apr 02, 2014 at 11:34 UTC ( #1080754=perlquestion: print w/replies, xml ) Need Help??
rustic has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have an unexpected behavior while doing :
use File::Path 'make_path'; File::Path->make_path($path); chdir $path or die "Cannot chdir: $!";
What happens here is that a "File::Path" directory is created inside each new directory. If I omit the chdir part, it does behave correctly i.e. an empty new directory.
Is this somehow wrong ? The perldoc File::Path gives me:
VERSION This document describes version 2.08 of File::Path, released 2009- +10-04.
According to the same perldoc there's a bug report page for it, but I want to discuss it here first.

Replies are listed 'Best First'.
Re: Recursive directories
by Jenda (Abbot) on Apr 02, 2014 at 12:08 UTC

    Drop the File::Path->.

    The point is that you are calling the make_path() as if it was a static/class method, while it's an ordinary, non-OO subroutine. When you write Foo->bar(...), Perl not only looks for subroutine named "bar" in the package "Foo" and its parent packages, but also passes the "Foo" to the bar() subroutine as the first parameter.

    Assuming the Foo package contains a bar() subroutine

    is thus equivalent to
    Foo::bar( 'Foo', 'hello');

    Enoch was right!
    Enjoy the last years of Rome.

Re: Recursive directories
by McA (Priest) on Apr 02, 2014 at 12:08 UTC

    I'm not sure why you think that you can call make_path as a class method?


    You hopefully know that the first argument to make_path will be the class name File::Path.


Re: Recursive directories
by rustic (Monk) on Apr 02, 2014 at 13:53 UTC
    Thanks for the responses but I guess there's a bit you still miss.
    I agree on the fact the make_path() will take one more argument though, my concern is why I do get different results if I do chdir and I when I don't after this line ?
    And, of course I have the $path to be created defined somewhere before.
    The reason I have called the subroutine as a class method was in first place due to the error:
    Undefined subroutine &main::make_path called at when I go with:
    use File::Path; make_path($path);
    To fix this I had to either:
    use File::Path 'make_path';
    or, to call it as a class method.

      Sorry, but I think you missed something:

      a) You must NOT use File::Path->make_path if you don't want to create directories named 'File::Path'. You can use

      use File::Path qw(make_path); make_path($dir);


      use File::Path; File::Path::make_path($dir);

      b) I can't see the reason for your chdir-problem from the three lines of code given. So you missed to provide some further context. At least you have to explain what you expected and what you got.


        I wouldn't still agree on the point a) I can run it the way I have written it without having created any "File::Path" directory. Something like that will work without problem:
        for (@list) { File::Path->make_path($path); }
        The problem strikes when I have to chdir into that directory to do something there, like create a file but that's not even necessary. This piece of code creates me the mentioned "File::Path" subdirectory compared to the above :
        for (@list) { File::Path->make_path($path); chdir $path or die "Cannot chdir: $!"; }
Re: Recursive directories
by cord-bin (Friar) on Apr 02, 2014 at 12:25 UTC

    File::Path->make_path is used to make a path, so you should define your $path before. If you try:

    use File::Path 'make_path'; $path = '/'; File::Path->make_path($path); chdir $path or die "Cannot chdir: $!";

    it will work and it does for me

    To get the path you can do something like:

    use File::Path 'make_path'; use Cwd; $path = Cwd::getcwd; File::Path->make_path($path); chdir $path or die "Cannon chdir: $!"; print $path. "\n":


      be careful. You probably get what you don't want. Jenda explained it very nice. When you do the following assuming you have the correct rights:

      use File::Path 'make_path'; File::Path->make_path('huhu');

      you create two directories: File::Path and huhu. Try it with:

      File::Path->make_path('huhu', { verbose => 1});

      after removing the newly created directories in you test cwd.


        Yes, you are right, it indeed created in a strage way two folders and is not what I meant as I didn't want the directory File::Path. Good to know, thank you for your point!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1080754]
Front-paged by Corion
[Discipulus]: ah ah I have two post in weekkly best! free coffe for all!
[Corion]: Gratulations! :-D

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-02-22 09:19 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (289 votes). Check out past polls.