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

.



Relevant Pages

  • How do you match 25 filenames?
    ... $FileName) depending on the name of a particular file. ... I am reading in ... time I see a particular filename pattern. ...
    (comp.lang.perl.misc)
  • Re: How hard is socket programming?
    ... I believe the first one is very good "easy reading" ... Non-Uniform Memory Access (NUMA) ... New NUMA Support with Windows Server 2008 R2 and Windows 7 ... 100% CPU usage. ...
    (microsoft.public.vc.mfc)
  • Re: Programming in standard c
    ... I consider having the text file size used for reading the file into ... memory to be used insufficiently often to make it worth caching it. ... size may suck significantly more than getting the binary-mode size ... NOT precalculate the size) and reallocing when needed ...
    (comp.lang.c)
  • Re: Noise Level of the PowerMac G5
    ... Or is it just a little bit more memory slots, ... If you are spending most of your time reading, thinking and editing ... I may have 10 to 15 apps open at once and switching between them a lot, ...
    (comp.sys.mac.misc)
  • Re: Large text file - in memory ( > 60mb)
    ... The file is over 64mb in size, reading it line by line to do a search ... while running the app, it would mean reading/searching the>64mb file many ... Then I have to show this record found (wich ... maybe creating a datatable to ease the search but I'm pretty sure memory ...
    (microsoft.public.dotnet.framework)