Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Directory creation in Perl using mkdir

by lancer (Beadle)
on Apr 04, 2011 at 08:03 UTC ( #897283=note: print w/ replies, xml ) Need Help??


in reply to Directory creation in Perl using mkdir

This method uses the OS shell for checking the existance of a directory:

$dirname = 'x'; $exists = ` if [ -d '$dirname' ] ; then echo 1; else echo 0; fi `; $exists =~ s/\n$//; if ($exists eq '1') { print "exists"; } else { print "not exists"; } print "\n";

Hope this helps...


Comment on Re: Directory creation in Perl using mkdir
Download Code
Re^2: Directory creation in Perl using mkdir
by cdarke (Prior) on Apr 04, 2011 at 11:48 UTC
    Why use a shell? However, if you want to, then it is much easier than that:
    $dirname = 'x'; system("[ -d '$dirname' ]"); # $? >> 8 (or the return value from system >> 8) is 0 on success if ($? >> 8) { print "does not exist"; } else { print "exists"; } print "\n";
    However this does not solve the issue with mkdir, so I can't see how using a shell would buy you anything.

      Thanks, cdarke, I was looking for a way to bring back the result of a shell command with the backtick operator... (and I didn't find it)

      So actually it's possible with the system() call.

      As for using the shell, it wasn't clear for me what was the original poster's problem with the mkdir() call, what was exactly failing.

      It appears to be a race condition from what he described, or it can be something else. By moving out the test of the directory's existance from Perl and into the shell, it's possible to eliminate the possibility of other errors inside Perl. I would have suggested to move out the `mkdir` command to the shell too.

      If the problem would still appear when the commands are executed in the shell, then it's not a Perl issue anymore.

      On the other hand, if moving out the commands to the shell would eliminate the issue, then it was a problem somewhere inside Perl, but it would be solved.

      My bet is that it's a problem with how Perl accesses the filesystem, and the "unless ( -d $dir ) { }" test is failing because it's not picking up the change fast enough (the creation of the dir by the other script instance), so it fails to detect it. Maybe it is some kind of a cache issue. (I have no idea, this is just a guess). But if this is the actual problem, then moving out the commands to the shell would probably solve it... (another guess). If not, then it's probably a real race condition.

        I see what you mean, but personally I would see errors in Perl on such basic and well-used build-ins would be unlikely. If it was a subtle timing issue then it is possible that a shell would appear to solve it because it would probably take longer. mkdir(1) is not a shell built-in normally (it might be on recent versions of ksh), so that would invoke two child processes just to run one kernel API call. A bit of an over-kill.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-19 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls