Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Unlink or rename?

by iphone (Beadle)
on Dec 22, 2010 at 06:23 UTC ( [id://878442]=perlquestion: print w/replies, xml ) Need Help??

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

I open a file to append some data but the next time when the script runs I want this file to be deleted so that the data doesnt get appended to the already existing file.I know there is an unlink command but I read somewhere that renaming would be a best option?I want to get the fellow monks opinion on this.Please let me know your suggestions

open(my $OUTPUT, '+>>', "my file.txt") or die $!; if (@array) { print $OUTPUT "@array"; } if (@array2) { pprint $OUTPUT "@array2"; }

Replies are listed 'Best First'.
Re: Unlink or rename?
by Anonymous Monk on Dec 22, 2010 at 06:38 UTC
    I open a file to append ... doesnt get appended

    If you don't want the data appended, why open in append mode?

Re: Unlink or rename?
by samarzone (Pilgrim) on Dec 22, 2010 at 06:39 UTC

    If you don't care for the old data why are you opening the file in append mode? Use '>' for opening file. You have also incorrectly written my where only file name should be. There is no value of a keyword inside double quotes if you are not evaling it. It should be something like

    open(my $OUTPUT, '>', "file.txt") or die $!;

    Don't forget to close the handle

    close($OUTPUT)

    Update: As pointed by hossman and Anonymous Monk below, "my file.txt" has no problem as a file name. My assumption was that you were expecting a file named "file.txt"

    -- Regards - Samar
      You have also incorrectly written my where only file name should be.

      my filename.txt is perfectly valid for a filename :)

      "my file.txt" is a perfectly valid filename on many filesystems.

        Agreed. I believed the OP did not want the file name as "my file.txt" and instead he wrote my as a keyword. If it was as a filename then that's perfectly fine.

        Thanks for pointing :)

        -- Regards - Samar
Re: Unlink or rename?
by MishaMoose (Scribe) on Dec 22, 2010 at 12:49 UTC

    Leaving aside whether you should have opened the file in appned mode or not and to answer your question ... unlink or rename ... it depends 8^) if you want the file to be available for something after you have completed your processing then rename. If you really want to delete the file then unlink. As other have mentioned in some detail ... opening the file in write moide wil lclear any previous contents and unless you are opening and closing the file multiple times in the script appending doesn't really gain you anything

    Hope this helps!

    Misha/Michael - Russian student, grognard, bemused observer of humanity and self professed programmer with delusions of relevance

Log In?
Username:
Password:

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

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

    No recent polls found