Chapter 17. Template-DrivenCode Generation
I’d rather write programs to write programs than write programs.
This chapter builds a template-driven code generator, an
indispensable tool in a C, C++, or Java programmer’s toolbox.
The chapter has two objectives: to make the case for code generation
as a method of code reuse and to present a small but nontrivial
problem that can exercise all the Perl concepts you learned in the
first half of the book: complex data structures, modules, objects,
and eval. Enjoy!
On Code Generation
Programmers create and use tiny
specification languages all the time. Database schemas, resources
(rc files in Unix such as
.mwmrc and .openwinrc),
user interface specifications (Motif UIL files), network interface
specifications (RPC or CORBA IDL files), and so on are all examples
of such languages. These languages enable you to state your
requirements in a high-level, compact, and declarative format; for
example, in Motif’s UIL (User Interface Language), you can
simply state that you want two buttons inside a form and spare
yourself the effort of writing 20 or so statements in C to achieve
the same effect.
The semantic gap between these specification languages and conventional systems-programming languages such as C or C++ can be bridged in one of two ways. The first approach is for the C application to treat the specification as meta-data; that is, the application embeds the specification parser and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access