http://www.perlmonks.org?node_id=339090


in reply to Re: Re: Templating system for generating C code?
in thread Templating system for generating C code?

The biggest advantage of using a template module over the HEREDOCS is the ease with which you can substitute the templates to generate differnt output for whatever reason you might need to:

  • Change the code fragments that are generated to test optimised C code
  • Switch the language that is generated (Or add another!)
  • Use the same information to build documentation/diagrams

The biggest advantage of encapsulating the "display" logic in the templates is that it simplifies the responsibilities of your application to:

  1. acquire input
  2. build the graph
  3. select a template
  4. pass the graph to the template
  5. write the output to disk
and insulates your application from bugs introduced by changing the display logic.

It also becomes much easier to produce all of those other formats that we might want because the templates now recieve all of the data in context and allow it to make output decicions based on the data rather than be an external holder to interpolated strings.

This would allow you to generate your C project many different ways without having to edit and re-edit versions of your application.

This allows you to maintain a parallel set of templates rather than "parallel" copies of the same application if you needed to test optimisation changes to your C code. Especially if changes to the output required changes to the display logic

generator.pl --template=c generator.pl --template=c-optimise-poll vs generator.pl generator_test_c-optimise-poll.pl

In the end what technique you decide to use depends on what you really want to do with the information you have.

If you don't need all the stuff, don't bother with it, but I like it when I can write a few templates and the beginnings of a documentation outline cheap.

It makes my manager happy to have documentation, and when my managed is happy I'm usually in a better mood :)

--
Clayton

Replies are listed 'Best First'.
Re^4: Templating system for generating C code?
by gnosti (Chaplain) on Dec 13, 2007 at 04:32 UTC
    I am building my first perl module. It helps my coding to be about to % INCLUDE something % and have the something be dynamically generated by perl code... like code for testing purposes as was mentioned above or parts of a Parse::RecDescent grammar, for me. TT makes it possiblel for me to break up a long program in which the scoping limits of 'our' forces me to keep the entire contents together.