Next Making Programs Faster 105

File Editing

        Subject: How to edit a file most efficiently?
        Date: 1998/04/27
        Message-ID: <3544E019.A1F7A6D6@shell.com>
        If I want to edit a file (say, remove all comment lines), I
        can do this:
        open IN, "myin.dat" or die: $!;
        open OUT, ">myout.dat" or die: $!;    
        while (<IN>)
         { print OUT $_ unless (/^#/);
         }
        close OUT;
        close IN;
        rename "myout.dat", "myin.dat";
        But this opens two files and does a rename. I suspect
        this won't be very efficient. Is there a better way?
        Thanks for any advice.

Next Copyright © 2003 M. J. Dominus