[Chicago-talk] Is this efficient.

Steve Peters steve at fisharerojo.org
Tue Oct 4 10:03:50 PDT 2005


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`;
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


More information about the Chicago-talk mailing list