Unless you're on a file system that allows for files which are sequences of lines (the only OS I know of which has such file systems and can run Perl is VMS), there are only two ways:
- Read the entire file into memory (for instance, into arrays), do all the slicing and dicing, and write out the result if you're done.
- Read in the file part by part, write it back to a different file after the slicing and dicing. When done, rename the new file to the original.