The book
Advanced Perl Programming has a whole chapter on this, and there is also
this article on perl.com. However, code generation (at least when it involves actual files, as opposed to just AUTOLOAD and eval magic) often indicates that you're doing something wrong. This has been widely discussed under the term "code smell." The gist of it is that your language should be able to factor out repetitive tasks without needing to actually generate code to do it. Also, code generation systems often lead to bad situations where people edit the generated code by hand and then can't generate it again without losing that work. So, use with caution.