macrosm4

How to change behavior according to other macro defined later?


Suppose I have a file tools.m4 with macro definitions:

define([YEAR_2_DIGITS], substr(YEAR, [2], [2]))

I want to use in another file query.sql:

changequote(`[', `]')
include(./tools.m4)

define([YEAR], [2017])

YEAR_2_DIGITS

But obviously it will return:

$ m4 query.sql


AR


instead of 17.

Is there a workaround to make that construct work?


Solution

  • I finally did it. One has to quote the body of the first definition:

    define([YEAR_2_DIGITS], [substr(YEAR, [2], [2])])