baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi,

i have this problem where i would like to create a path and directories automatically. my script is :

use strict; use lib "./Lib"; use Main; my $control = Main->new(); print "finished \n";
so when i initialize Main, the sub that checks if there is a path to my specific variable is started. like :
package Main; use strict; use File::Path qw(make_path remove_tree); ################################################## sub new { ################################################## my ($class)=@_; craete_path(); my $hash = {}; bless ($hash,$class); } sub create_path { # here it first checks if the path already exists or not, and if ther +e is no path, then it creates a path (with all necessary directory's) +. } 1;
so my question is : do people tend to do it like this or is there some other more practical/elegant way to do it ?

thanks

Replies are listed 'Best First'.
Re: how to create the path automatically
by moritz (Cardinal) on Jun 18, 2009 at 12:34 UTC
    I don't understand why you need to create an object and call to subs/methods before actually calling mkpath. What's wrong with simply writing:
    use File::Path qw(mkpath); my $path = 'path/to/create'; mkpath($path) unless -e $path;
      mkpath($path) unless -e $path;

      The unless clause is not necessary here. Further, this would die if the path can't be created, so unless the OP wants this exception to propagate, it's maybe better written as

      eval { mkpath($path) }; if($@) { ... }

      -- 
      Ronald Fischer <ynnor@mm.st>
      now this is a good question, and the answer is : because i didn't think this through , i just automatically copy/past/edit a peace piece (i was learning my english by VUK :) ) of my code.

      sorry (++)

        _____ .-' | '-. .' | '. / | \ ; | ; | | | of code -.- ; /|\ ; \ /` | `\ / './` | `\.' '-.__|__.-'


        holli

        When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.
Re: how to create the path automatically
by rovf (Priest) on Jun 18, 2009 at 12:42 UTC

    The function is called mkpath, not make_path. Also, you don't need to check for the existence; mkpath takes care of this by itself.

    -- 
    Ronald Fischer <ynnor@mm.st>