>>>>> "DB" == Dan Boger <[EMAIL PROTECTED]> writes: 
 
DB> This code was just written to see how things work.  I find that 
DB> understanding these kind of edge cases helps overall. 
 
In this case, I think you've stumbled across a known bug, so while it 
does reveal some implementation details of perl, I'd say the only 
lesson to take away for becoming a better Perl programmer is "don't do
that". 
 
Consider the following variation, with the output it gives (on my 
system): 
 
use Data::Dumper;  
@headers = ("header 1","header 2","header 3");  
@body = ("body A","body B","body C");  
$h = 4;  
foreach (@headers,"",@body) {  
    print "$h: $_\n";  
    @body = ();  
    push @unrelated, "something else " . $h; # <== NEW 
    push @headers,"header ".$h++;  
}  
 
4: header 1 
5: header 2 
6: header 3 
7:  
8: something else 4 
9: header 4 
10: something else 5 
 
With more exploration, you'll probably be able to trigger the
following error out of perldiag (if your perl version is new enough to
have it, otherwise you probably get a segfault):
 
       Use of freed value in iteration 
           (F) Perhaps you modified the iterated array within the 
           loop?  This error is typically caused by code like the 
           following: 
 
               @a = (3,4); 
               @a = () for (1,2,@a); 
 
           You are not supposed to modify arrays while they are being 
           iterated over.  For speed and efficiency reasons, Perl 
           internally does not do full reference-counting of iterated 
           items, hence deleting such an item in the middle of an 
           iteration causes Perl to see a freed value. 
 
 -- Stephen 
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to