Yes that did it. I still don't for the life of me see where $_ is getting updated in the sample code. Isn't $i the index in the sample? Maybe it's some Perl voodoo/arcana I don't understand.
I ... don't ... see where $_ is getting updated in the sample code.
In a Perl-style for-loop like for (0 .. $max) { ... }
the scalar variable $_ is the implicit loop or "topicalized" variable.
(Update: See Foreach Loops in perlsyn.)