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

Hello!
I am having a very noob moment. I am trying to create a file called "example" in a directory that contains an already existing directory called "example" e.g.

path/example #file<br> path/example #directory<br><br>

as you can see there already exists a directory called example where i am trying to create a file called example and it fails on "file exists" code:
make_path ('path/example'); open ( my $fh, '>', 'path/example' );

I have beat my head against the wall and the only solution i can come up with is creating the "example" FILE in a temp directory then copying it over to its example directory. But this does a couple of processes that i /could/ live without if i could figure out why its doing this.

Replies are listed 'Best First'.
Re: File::Path problem (will not create file if directory with same name exists)
by choroba (Cardinal) on May 18, 2014 at 21:54 UTC
    By opening a file in the "read" mode ('<'), you aren't creating anything. Show us the code that worked for you, because as far as I know, it's not possible to have a file and a directory of the same name in the same location.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      funny thing is i been hung up on this particular problem for about 5 hours now trying to figure out what was wrong with my code lol
        Hello, james28909.

        FWIW cAsE matters. Meaning; if it should help, you could Write Example into the same directory.

        Perhaps obvious. But thought it worth mentioning, just the same. ;)

        --Chris

        ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      apologies, i am actually using '>'. updated original question
        The second part of my reply still holds. It's not possible. If it were possible and you told your system to open path/example, which one should it open?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      oh i did not even know you couldnt create a file and directory of the same name in the same directory. seems i have to make a temp folder and then copy the file to the right location. thanks for pointing that out. real noob moment lol

        Hint: Create a file with a temporary filename in the correct location, write the file's content and attributes and if everything went well rename it into the correct filename. If something fails delete the file.

        Of course you can create the tempfile somewhere else, but then the copy operation may fail (not enough diskspace? insufficent permissions?) which could leave you with an incomplete/invalid file. Reducing the 'critical' operation to a rename drastically reduces the chances of something going wrong.