Results 1 to 9 of 9
Is there a simple function to clear the contents of a text file in Perl? All of the documentation I've seen on file manipulation tells you how to delete the ...
- 08-14-2006 #1
Deleting Text in Perl
Is there a simple function to clear the contents of a text file in Perl? All of the documentation I've seen on file manipulation tells you how to delete the file itself (i.e. unlink()) but I need the file to remain in existence, just with nothing in it. Thanks for your help.
- 08-14-2006 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,065
Hi.
Experiment with the truncate function ... cheers, drl
Code:truncate filehandle, length truncate expr, length
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-15-2006 #3
Also, in most languages, if you open a file for writing, it gets truncated right away.
Flies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794
- 08-15-2006 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,065
Hi, Javasnob.
Yes, good point.
The truncate function provides more flexibility as well as conveying the developer's intention to the reader, but I do like the simplicity of open-write.
I quickly checked truncate and open-write in perl, and they both performed as expected, keeping the same inode for the file, effectively emptying the file (I seem to recall that we had used the phrase zeroing a file for such an operation, as in a null command, but with a re-direction, just ">t1", for example) ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-15-2006 #5
What if I want to clear the text from a file that I'm reading from? Do I have to open it in a different way? Basically, what I'm doing is reading all of the lines from a file and when I'm done I want the file to be empty. Also, I can't seem to figure out what the second parameter of the truncate function is. The documentation says length but is that lines or characters or what? Thanks again for your help.
- 08-15-2006 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,065
Hi.
The second argument on truncate is the number of characters to which you wish to truncate the file. I tested this by creating a short file, then running a perl program that opened the file, read the contents into an array, used truncate with a request of 0 characters, and it worked as I expected (I also truncated to a non-zero length to be sure that it was a character length). I did notice one anomaly, and I haven't had time to investigate it fully -- the truncate function did not seem to work when I used the filehandle, only when I used the string that was the filename (I used a scalar to hold the string).
You could also try the open-write method -- so you'd open the file, read it, close it, open it for write, close it.
I suggest you write a little script or two to test these methods before embedding it into a longer code. That way you can feel confident that possible problems are not due to the truncate ... cheers, drl
( edit 1: typo )Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-15-2006 #7Ah, thanks drl. That was my problem, I was using the file handle. Worked when I used the filename. Strange.
Originally Posted by drl
- 08-15-2006 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,065
Hi.
I have an answer, not thoroughly tested, but it makes sense.
To use function truncate on a filehandle seems to require that the file be open with write permission. That seems to be right (or write, as the case may be
).
My test was to unlink a file to make sure it did not exist, open it for write, put some data on it (more than 10 characters), then truncate it (I used 10 characters), finally closing it -- and it did that exactly. There may be other factors involved, but I'm OK with this answer ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-15-2006 #9
I'm OK with using the explicit file name. I can't unlink the file so I'll just do it that way. Good to know the reason for it though.
Thanks.


Reply With Quote
