Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Phantom directories created using mkdir

by jonnyfolk (Vicar)
on Feb 19, 2003 at 16:13 UTC ( [id://236684]=perlquestion: print w/replies, xml ) Need Help??

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

I am using mkdir to create a directory:
$dir_name = "/path/$scalar"; mkdir $dir_name, 0777 unless -d $dir_name;
On occasions this created false directories on the occasions that $scalar wasn't available. I used a conditional to prevent this:
if ($scalar) { $dir_name = "/path/$scalar"; mkdir $dir_name, 0777 unless -d $dir_name; } else { die "Sorry, no scalar value available: $!" }
However I see that phantom directories are still being created (with the name $scalar). On trying to open the directories I get "server response: no such file".

How can I prevent this happening an what can I do to remove these 'phantom' files?

Replies are listed 'Best First'.
Re: Phantom directories created using mkdir
by jasonk (Parson) on Feb 19, 2003 at 16:34 UTC

    Something else is happening in your code that isn't demonstrated here, the only way you could get a directory named '$scalar' is if either you used single quotes when creating $dir_name, preventing the variable from getting interpolated, or the variable $scalar contains the word $scalar. If $scalar were simply empty, the code you have here would be checking /path/ and not creating the directory.

    If this is a unix machine you should be able to remove that directory by using rm ./\$scalar, or perl -e "unlink('$scalar');"

      I'm sorry - I've obviously been more than usually obtuse in the phrasing of this question.

      when I wrote that the directory is named $scalar I meant that the scalar $scalar had interpolated properly and had been given its correct name. Thus if $scalar=1000 , the directory would be found at /path/1000

      As for deleting the 'phantom' directories - the problem is that the (Unix) server states that they don't exist! I can see them via my ftp client.
Re: Phantom directories created using mkdir
by CountZero (Bishop) on Feb 19, 2003 at 16:32 UTC

    To get a better understanding of your question, what do you mean by "the occasions that $scalar wasn't available"?

    Do you mean that $scalar was empty or undef or not yet existing or not in this scope or ... ?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      I mean that $scalar was undefined. This was actually due to a faulty line of code where $scalar wasn't being passed properly. I fixed that problem but left the conditional in place as insurance!
Re: Phantom directories created using mkdir
by OM_Zen (Scribe) on Feb 19, 2003 at 16:39 UTC
    Hi ,

    The $scalar , is it defined using a my (should be) you should use like

    if ($scalar ne ""){ $dir_name = "/path/$scalar"; mkdir $dir_name, 0777 unless -d $dir_name; }else{ # print " the error and exit $! \n"; }


    As I have only this part of your total post , I guess you are checking for the definition rather than the value of the scalar variable

      Yes, absolutely. I can see that what you are suggesting is better than what I had and will incorporate it. thanks for your help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found