Here's (roughly) what Perl does when you use the
-pi.bak flag on the file
filename (and what you'll have to emulate):
- Open the file "filename" for read, with handle READ
- Rename "filename" to "filename.bak"
- Open the file "filename" for write+truncate with handle WRITE
- Read through all the lines from READ, modify them, then print them to WRITE