Of course, this would make answering the question a lot easier and would also help me to identify further problems. However, having spent the last hour trying, I can't reproduce the problem with a simplified version.
Futhermore, I've actually now fixed the problem in the real application. Unfortunately, I'm not sure exactly what it was. I had a nested subroutine which was calling substr on a string variable from its outer scope; this variable contained the character data being chunked. I tried moving the procedure encoded in that subroutine into inline statements of the enclosing subroutine, thus dispensing with the nested subroutine, and now the problem does not occur. Possibly it was some sort of scoping problem, such as an unintended closing over that string variable?