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

Recursive directories

by rustic (Scribe)
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.

Comment on Recursive directories
Select or Download Code
Re: Recursive directories
by McA (Curate) 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?

    File::Path->make_path($path);

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

    McA

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

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

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: Recursive directories
by cord-bin (Pilgrim) 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":

      Hi,

      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.

      McA

        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!
Re: Recursive directories
by rustic (Scribe) 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);

      or

      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.

      Regards
      McA

        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: $!"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-10-26 01:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls