echo "#!/bin/bash" > program for ((I=1;I<=992;I++)) do
- metaprogramecho "echo $I" >> programdone chmod +x program
This script (or program) generates a new 993 line program which prints out the numbers 1-992. This is only an illustration on how to use code to write more code, this is not the most efficient way to print out a list of numbers.
A good programmer can write and execute this metaprogram in 5-10 minutes, and will have generated exactly 1000 lines of code in that amount of time. This is a good reason why you should never pay programmers by the number of lines of code they write.
The most common metaprogramming tool is a compiler which allows a programmer to write a relatively short program in a high-level language and uses it to write an equivalent assembly language or machine language program. This generally saves a good deal of time compared to writing the machine language program directly. (see also this compiler_metaprogramming_example)
Another still fairly common example of metaprogramming might be found in the use of lex (see also: flex) and yacc (see also: bison), which are used to generate compilers and interpreters.
Metaprogramming is also the name of an management technique invented by Charles Simonyi for organizing the work of a team of programmers. In a 2002 news item , The Age noted: