Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Deleting on Files

by Anonymous Monk
on Feb 28, 2007 at 16:06 UTC ( #602529=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Hi there, my question is about how I could open a file and delete its content, but not the file itself. The reason is that my program opens a large text log file and if this file is larger than 5MB my program zips it and delete whatever is above 5MB, but it is leaving behind some junk left over from the over size file. I decided that the best way is to get the size of this log file and if it is larger than 5MB just zip this file, store the zip file somewhere, than open the original log file and delete the contents of it. I am having a problem open this file just to delete its content.

Thanks for the Help!!

Here is just a sample of how open a file, but what about deleting its content?

#!/perl/bin/perl use strict; use warnings; open (FILE, '<', 'sample.txt') or die "Could not open sample.txt: +$!"; while (<FILE>) { ....????? } close (FILE) or die "Could not close sample.txt: $!";

Replies are listed 'Best First'.
Re: Deleting on Files
by philcrow (Priest) on Feb 28, 2007 at 16:09 UTC
    This problem is almost always better solved with a standard tool like logrotate.


Re: Deleting on Files
by Albannach (Prior) on Feb 28, 2007 at 16:48 UTC
    While philcrow undoubtedly has the right approach to this problem, I'm surprised nobody has mentioned Perl's own truncate function, which works just fine in Win32 and is neater than the open solutions.

    Update: there is no need to open the file, as truncate will operate on the file name, e.g. truncate 'myfile.log', 0; will set the length of the file to zero, but you can use any desired length in that place.

    I'd like to be able to assign to an luser

Re: Deleting on Files
by friedo (Prior) on Feb 28, 2007 at 16:10 UTC

    Simply open the file for writing -- that will automatically truncate it.

    open FILE, '>', 'sample.txt' or die "Could not open sample.txt: $!"; close FILE;

    Update: I second the recommendation for logrotate.

Re: Deleting on Files
by bart (Canon) on Feb 28, 2007 at 17:16 UTC
    If you want to change the contents of a file, you'll have to open it in read/write mode. That's '+<', plain '<' won't do.

    And when that is changed, you can use truncate on the handle.


    open FILE, '+<', $filename; truncate FILE, 5*1024*1024 if -s FILE > 5*1024*1024; close FILE;

    But, I'm not convinced that this is the best approach to solve your problem: in general, log files are appended to, at the end, and if you shorten the file you'll remove the latest endtries. People usually want to delete the older entries — but you can't do that without rewriting the entire file.

    Perhaps you should look into rotating your log files instead?

Re: Deleting on Files
by andye (Curate) on Feb 28, 2007 at 16:13 UTC
    Phil's point about logrotate is well-made, but:

    Why open it when you can clobber it?

    If you're on a unix machine you could do:  echo '' > filename

    In Perl the equivalent would be to open the file in a way that clobbers the contents.

    Hope that helps,


      No, I am in a Windows box!
        Then you can still use Perl's open() in clobbering mode.
        Someone else has given an example.



        Could I do something like this:

        #!/usr/bin/perl use strict; use warnings; open (FILE, '+<', 'sample.txt') or die "Could not open sample.txt: + $!"; close (FILE) or die "Could not close sample.txt: $!";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://602529]
Approved by kyle
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-06-20 08:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.