Perl code for working with templates (mostly)