From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken Sent: 07 June 2012 23:08 To: Perl-Win32-Users@listserv.activestate.com Subject: win32 and modifying a file
> dear win32 perl users, ive never actually known how to 'modfiy' a file using > perl on win32. up till now, ive > always read file1, found the data i > intended to change, and have always created a new file 2 containing the > changes. if i wanted to 'simulate' changing file1, when done i could rename > file2 to file1. in other words, > ive never learned how to modify a file > directly. > > ive read win32 makes it more difficult to do this than on unix os's. > > but in any case, today i wanted to ask the group. > > assumming 'file1' exists with the following 3 records in it: > > A > B > C > > is there a 'simple' code fragment someone could post that would demonstrate > iterating through this file and > when record 'B' is encountered, we want to change 'B' to 'B_modified' - done > in a way where we only access > 'file1'. maybe this cant be done, but im asking. > > yes i do realize there is another approach, upserp contents of 'file1' modify > in memory, delete 'file1', then > recreate it by dumping the in-memory > modified contents. this seems more like a 'hack' than a direct > manipulation of the original file. I would call editing a file in place, without a backup, more of a hack. Depending on how valuable your data is, making it possible to perform a rollback in the event of a problem seems sensible. So, however you do it, keeping a backup of the original may be a good idea. As for the how to do it part, Tie::File has already been suggested, which may be a good fit for what you want to do. If your file is small enough to fit in memory, then File::Slurp may be worth a look, particularly the edit functions. HTH -- Brian Raven Please consider the environment before printing this e-mail. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs