in reply to recursive mkdir

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

And, no, I can't see this as a good case for using recursion. Sure, you could code it recursively, but the value of recursion is when it makes coding it easier. I think it would almost be a stretch to do this one recursively so you'd just be working hard to make your code slower. (:

FYI, see (tye)Re: Recursion for my thoughts on recursion.

This node has been updated.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: recursive mkdir
by zzspectrez (Hermit) on Jan 08, 2001 at 10:56 UTC

    Thanks for the suggestion. I hadnt come across that module. Ill have to check it out, Im interested how the module does it.

    I used recursion, because it was the only obvious way I could think of doing it. One question. How much slower could the code be when dealing with creating subdirectories? Since realisticly you are not going to create directories of say 100 deep. The script Im using it in seems to work quickly.

    THANKS!
    zzSPECTREz

      Yeah, sorry, I was just noticing that you had coded it recursively. I didn't notice the recursion and had interpretted your posting as, in part, asking how you would go about rewriting it recursively (to learn about recursion). :-}

      No, doing this recursively isn't going to make this unacceptably slow. I think the reason that you ended up with a recursive solution is that your first idea at how to loop through the list of directories went in the "wrong" order (or you chose this order because you wanted to be optimistic and assume that most of the time you don't need to create many directories so start from that end).

      Recursion is one way to reverse a loop, but there is usually a "better" way. In fact, in Perl you can use the built-in "stacks" to reverse even the most hard to reverse loop via:

      my @stack; while( <> ) { push @stack, $_; } while( @stack ) { local( $_ )= pop @stack; # your code here }
      And it is hard to argue that the above is worse than recursion in any way. ;)

              - tye (but my friends call me "Tye")