in reply to Next & Last - Perl's Expect

You are trying to next in a loop outside of the scope in which the next is executed. The 'next' occurs in a subroutine which has no contextual relevance to the containing loop.