Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
go ahead... be a heretic
 
PerlMonks  

Problem with make_path in File::Path

by Anonymous Monk
on Jul 20, 2012 at 16:12 UTC ( #982862=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey,

The documentation states: "The make_path function creates the given directories if they don't exists before.

Still,

make_path $path or die "Couldn't create tmp dir: $!\n";
keeps on giving me plenty of "Couldn't create tmp dir: File exists".

I even tried surrounding the whole thing with if (!-e $path) { ... }, but of course that doesn't help in case of a race condition, since several instances of the script are forked off in my code. But shouldn't make_path deal with this by default?

I need to sit down and work through the "Error handling" part because I don't understand it yet, but will that help at all in my case, with several instances of the script all trying to create the directory before writing to it?

Comment on Problem with make_path in File::Path
Download Code
Re: Problem with make_path in File::Path
by MidLifeXis (Prior) on Jul 20, 2012 at 16:24 UTC

    Let's assume the simplest case - you run the make_path call with the same parameters twice in a row. Assume that the first time the $path does not exist. The second time, since the $path already exists, the function returns a value of (), which is false, triggering the die.

    See the documentation for File::Path for more details.

    Update: Change the return value from 0 to ()

    --MidLifeXis

      ***argh***

      I'll admit it is implicit in "...in scalar context (the function returns) the number of directories created", but there is no way I would have got it without your help! Reading between the lines that way presupposes much more experience with Perl.

Re: Problem with make_path in File::Path
by toolic (Chancellor) on Jul 20, 2012 at 17:14 UTC
    Keep reading the doc for File::Path:
    The function returns the list of directories actually created during the call; in scalar context the number of directories created.
    If make_path makes a directory which didn't already exist, it returns 1, which means the die is not executed.

    If make_path does not need to make a directory because it already exists, it returns undef, which means the die is executed. But, you do not want it to die because you are emulating 'mkdir -p'.

    Just use:

    make_path $path;

    Or, use error => \$err as specified in the docs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982862]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2013-05-25 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (516 votes), past polls