The Template Toolkit uses a failsafe counter to prevent runaway WHILE loops which would otherwise never terminate. If the loop exceeds 1000 iterations then an undef exception will be thrown, reporting the error: WHILE loop terminated (> 1000 iterations) The $Template::Directive::WHILE_MAX variable controls this behaviour and can be set to a higher value if necessary.