Re: file handle in perl




Hi Irfan,


Irfan Sayed wrote:
I am struggling with file handling in perl. i want to do following operation .

1: open the file
2: read the file
3: do the changes in specific lines
4: save the file
5: close the file


Does this FAQ help?

Q: How do I change, delete, or insert a line in a file, or append to the beginning of a file?

http://perldoc.perl.org/perlfaq5.html#How-do-I-change%2c-delete%2c-or-insert-a-line-in-a-file%2c-or-append-to-the-beginning-of-a-file%3f

The file that you read from and the file you write to should be two different files, if you want to make things easy for yourself. If you insist they have to be the same files, then just do a "rename" at the end.

Depending on the size of your file, you might want to see if "reading the entire file at once" is something you want to try. In that case, it would be ok to save to the same filename since you've read everything into memory before you wrote anything out. See:

http://perldoc.perl.org/perlfaq5.html#How-can-I-read-in-an-entire-file-all-at-once%3f

Ray

.