Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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:

Monks!!!

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: $!";

Comment on Deleting on Files
Download Code
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.

    Phil

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 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,

    andye

      No, I am in a Windows box!
        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: $!";
        Then you can still use Perl's open() in clobbering mode.
        Someone else has given an example.

        Best,

        andye

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 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.

    Untested:

    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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-12-18 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls