M4: Getting Started
According to m4's official online documentation:
m4 is a macro processor, in the sense that it copies its input to the output, expanding macros as it goes. Macros are either builtin or user-defined, and can take any number of arguments. Besides just doing macro expansion, m4 has builtin functions for including named files, running shell commands, doing integer arithmetic, manipulating text in various ways, performing recursion, etc.... m4 can be used either as a front-end to a compiler, or as a macro processor in its own right.
Linux would have it installed by default, windows binaries can be obtained here.
So seriously what is it?
Command Prompt: > m4
define(NAME, Amit)
NAME
Amit
define(AGE, 24 years)
My age is AGE.
My age is 24 years.
format('result is %d', eval(2*10))
'result is 20'
define(full_name, NAME upadhyay)
full_name
amit upadhyay
syscmd(dir *dbg)
Volume in drive C has no label.
Volume Serial Number is 7C69-CA73
Directory of C:\Documents and Settings\amitu
06/22/2007 03:29 PM <DIR> bin-dbg
0 File(s) 0 bytes
1 Dir(s) 1,825,427,456 bytes free
define(`foo', `Macro `foo'.')dnl things after dnl are comments.
foo
Macro foo.
Things in purple are input, rest is output.
Why not your fav programming language? For most simple cases, m4 is much easier to read and understand, and you don't have to write and store scripts everywhere. Think of it as a simple templating language.
A disclaimer:
Some people find m4 to be fairly addictive. They first use m4 for simple problems, then take bigger and bigger challenges, learning how to write complex sets of m4 macros along the way. Once really addicted, users pursue writing of sophisticated m4 applications even to solve simple problems, devoting more time debugging their m4 scripts than doing real work. Beware that m4 may be dangerous for the health of compulsive programmers.
:-)
Labels: Programming Tips n Tricks
If you find this post useful, please conside buying me a pizza!


0 Comments
<< Home