Next: Builtin Prev: Pushdef Up: Definitions

5.7 Indirect call of macros

Any macro can be called indirectly with `indir':

 -- Builtin: indir (NAME, [ARGS...])
     Results in a call to the macro NAME, which is passed the rest of
     the arguments ARGS.  If NAME is not defined, an error message is
     printed, and the expansion is void.

     The macro `indir' is recognized only with parameters.

   This can be used to call macros with computed or "invalid" names
(`define' allows such names to be defined):

     define(`$$internal$macro', `Internal macro (name `$0')')
     =>Internal macro (name $$internal$macro)

   The point is, here, that larger macro packages can have private
macros defined, that will not be called by accident.  They can _only_ be
called through the builtin `indir'.

   One other point to observe is that argument collection occurs before
`indir' invokes NAME, so if argument collection changes the value of
NAME, that will be reflected in the final expansion.  This is different
than the behavior when invoking macros directly, where the definition
that was in effect before argument collection is used.

     $ m4 -d
     define(`f', `1')
     f(define(`f', `2'))
     indir(`f', define(`f', `3'))
     indir(`f', undefine(`f'))
     error-->m4:stdin:4: undefined macro `f'

   When handed the result of `defn' (Note: Defn) as one of its
arguments, `indir' defers to the invoked NAME for whether a token
representing a builtin is recognized or flattened to the empty string.

     $ m4 -d
     indir(defn(`defn'), `divnum')
     error-->m4:stdin:1: Warning: indir: invalid macro name ignored
     indir(`define', defn(`defn'), `divnum')
     error-->m4:stdin:2: Warning: define: invalid macro name ignored
     indir(`define', `foo', defn(`divnum'))
     indir(`divert', defn(`foo'))
     error-->m4:stdin:5: empty string treated as 0 in builtin `divert'

automatically generated by info2www