in reply to Using Push and Pop.

This will demonstrate why popping items off of the list you are iterating over is a bad idea:
use strict; use warnings; my @list = 'a'..'z'; for my $letter (@list) { my $popped = pop @list; printf "letter: %s popped: %s list: %s\n",$letter,$popped,join('', +@list); }
Output:
letter: a popped: z list: abcdefghijklmnopqrstuvwxy letter: b popped: y list: abcdefghijklmnopqrstuvwx letter: c popped: x list: abcdefghijklmnopqrstuvw letter: d popped: w list: abcdefghijklmnopqrstuv letter: e popped: v list: abcdefghijklmnopqrstu letter: f popped: u list: abcdefghijklmnopqrst letter: g popped: t list: abcdefghijklmnopqrs letter: h popped: s list: abcdefghijklmnopqr letter: i popped: r list: abcdefghijklmnopq letter: j popped: q list: abcdefghijklmnop letter: k popped: p list: abcdefghijklmno letter: l popped: o list: abcdefghijklmn letter: m popped: n list: abcdefghijklm
By the time you reach letter 'm' you have removed enough items from the end of the list that you are now at the new end.

Update - added quote from perlsyn

 If any part of LIST is an array, "foreach" will get very
 confused if you add or remove elements within the loop
 body, for example with "splice".   So don't do that

Replies are listed 'Best First'.
Re^2: Using Push and Pop. - response to update
by ikegami (Patriarch) on Aug 23, 2006 at 22:28 UTC
    Based on that perlsyn quote, another (inefficient) solution would be to convert the array into a list:
    foreach (map $_, @file) { my $test = pop (@file); print $test; }