1: I did this to see if it helped performance. It didn't, but I didn't change it back before posting.
2-3: I'll try these and see if they do anything interesting.
4: Yeah, I know. I've got a seriously hacked up copy of CGI::FastTemplate that I've been using for years, so it has a style that I've gotten used to and, more importantly, that the Web page designers I work with have gotten used to. It's also a PITA to maintain, so I rewrote it to be more legible (and I hoped faster, but it turns out not). I'm trying to decide if I should keep my hacked up copy of CGI::FastTemplate, use my rewritten replacement, or just use another engine. That got me started benchmarking different templating engines, which led to optimizing this sub. Maybe when I've done a little more research I'll Seek some Wisdom on the merits of the different templating engines...