in reply to Perl Script to write a perl script

To properly process your template by your specs, you'd need to parse Perl. That's no small task. Don't reinvent the wheel. Use Template-Toolkit or something.