[Chicago-talk] Is this efficient.

Jonathan Steinert hachi at kuiki.net
Tue Oct 4 10:04:37 PDT 2005


Steve Peters wrote:

>On Tue, Oct 04, 2005 at 09:41:20AM -0700, Richard Reina wrote:
>  
>
>>I have a short program opens a file and adds a line
>>from when a certain event takes place.  So the file
>>does not get too big I remove the 1st line in the file
>>whenever one is added.  The only way I know how to do
>>this is:
>>
>>system("perl -ni.bak -e 'print unless 1..1'
>>text.txt");
>>
>>Is it ineficient to have this system call?  If so how
>>can I modify the code to work w/o the system (from
>>with the program)?  
>>
>>    
>>
>
>If you are always removing the first line, you should only have one line
>in the file at any time.  So ...
>
>my $file = "filename";
>my $mv = "mv $file $file.bak";
>`$mv`;
>  
>

For the record... no reason to fork for this, the 'rename' function
already exists in perl.

rename $file, "$file.bak";


>open FOO, ">", $file || die "Can't open $file: $!";
>print FOO "Stuff to print out\n";
>close FOO;
>
>Note: I'm not checking for success or failure for the "mv", since I'm not
>sure of your requirements since you aren't checking system() for errors.
>
>Steve Peters
>steve at fisharerojo.org
>_______________________________________________
>Chicago-talk mailing list
>Chicago-talk at pm.org
>http://mail.pm.org/mailman/listinfo/chicago-talk
>  
>




More information about the Chicago-talk mailing list