I made a comment in my last post that I thought deserved more comment: "here, as usual, doing it right means doing it like Perl". I do like Perl a lot, but I came to it quite late in the game. In the beginning it wasn't much better suited to writing large programs than bash.
For a long time, C required that all variable declarations go at the top of a block. PHP, well, PHP is still a ridiculous mess, but it's getting better. To quote Pat V., "It's painful to watch PHP make all the same mistakes Perl made 10 years ago." C++ compilers still have some compatibility issues, but even a young guy like me remembers a time when it was even worse, where many compilers barfed on simple template code and the STL was slow and unreliable in many installations. And I can only imagine those poor early Lisp programmers that had to use S-expressions for everything... OK, scratch that last one.