I'm fairly new to perl, and I was playing around with a self-writing file, which looks like this:
If it worked as I intended, it would append a copy of the file, excluding the first four lines, to the end of it. That is, after running it once, there would be two while-loops, instead of one.
Running it yet again should produce 6 while-loops: the two that were already there, and then two more from each of the two while loops.
This is exactly what happened. However, running it a third time, instead of producing 42 while-loops, created an infinite loop...by the time I had stopped it, I had a 300MB text file of while-loops!
Just out of curiosity, I manually created files with multiple while-loops of the above code. Five while-loops produced 30 while loops, as expected, but the manually created 6 while-loops also sent the program into an infinite loop.(As did any number greater than 6).
So anyway, I'm wondering if any of you have an idea what is going on here. I'm suspecting it has something to do with how the perl interpreter reads in files, but I don't know nearly enough about how it works to try to understand it.
Thanks for any help you can give.
Code:
#!/usr/bin/perl -w
open Self, "myFile" ;
open DaFile, ">>myFile";
my $i = 0;
while (my $temp = <Self>) {
print DaFile $temp if ($i>4);
$i++;
}
$i = 0;
close Self;
open Self, "myFile" ;
#########11111111111##########
If it worked as I intended, it would append a copy of the file, excluding the first four lines, to the end of it. That is, after running it once, there would be two while-loops, instead of one.
Running it yet again should produce 6 while-loops: the two that were already there, and then two more from each of the two while loops.
This is exactly what happened. However, running it a third time, instead of producing 42 while-loops, created an infinite loop...by the time I had stopped it, I had a 300MB text file of while-loops!
Just out of curiosity, I manually created files with multiple while-loops of the above code. Five while-loops produced 30 while loops, as expected, but the manually created 6 while-loops also sent the program into an infinite loop.(As did any number greater than 6).
So anyway, I'm wondering if any of you have an idea what is going on here. I'm suspecting it has something to do with how the perl interpreter reads in files, but I don't know nearly enough about how it works to try to understand it.
Thanks for any help you can give.