m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

named arguments in m4?


From: Daniel Goldman
Subject: named arguments in m4?
Date: Sun, 20 Apr 2014 15:36:50 -0700
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0

Hi,

Any chance m4 could (or would want to) support named arguments? In other environments, I usually prefer named arguments over $n arguments.

Take the following example with $n arguments:

$ cat temp.m4
m4_define(EXCHANGE, $2 : $1)m4_dnl
EXCHANGE(`Arg #1', `Arg #2')

$ m4 -P temp.m4
Arg #2 : Arg #1

Maybe m4 could allow named arguments, something similar to (not suggesting this currently works):

$ cat temp.m4
m4_define(EXCHANGE, $arg_2 : $arg_1, arg_1, arg_2)m4_dnl
EXCHANGE(`Arg #1', `Arg #2')

$ m4 -P temp.m4
Arg #2 : Arg #1

This would be somewhat analogous to cpp, which allows named arguments, and which has a nice syntax (IMO) in this situation:

$ cat temp.h
#define EXCHANGE(arg1, arg2) arg2 : arg1
EXCHANGE(Arg #1, Arg #2)

$ cpp -P temp.h
Arg #2 : Arg #1

For the short examples shown above, it hardly matters. But for longer macro expansions, variable names like $width, $height, $file_name are a LOT better than $1, $2, $3.

When I write shell scripts, I always assign $1, etc. to named variables. Same with C and argv[1], etc. I experimented some, and see there is at least one way to do this in m4:

$ cat temp.m4
m4_define(MULTI_LINE_MACRO_FOR_CREATING_HTML_BLOCK, `m4_dnl
m4_define(`WIDTH', $1)m4_dnl
m4_define(`HEIGHT', $2)m4_dnl
m4_define(`FILE_NAME', $3)m4_dnl
<img align="left" border="1" vspace="0" hspace="10"
  width="WIDTH" height="HEIGHT"
  src="/vn/ro/images/FILE_NAME"
  alt="Photograph above Navigation"
  title="Photograph above Navigation"
  >')m4_dnl
MULTI_LINE_MACRO_FOR_CREATING_HTML_BLOCK(100, 200, file1.jpg)

$ m4 -P temp.m4
<img align="left" border="1" vspace="0" hspace="10"
  width="100" height="200"
  src="/vn/ro/images/file1.jpg"
  alt="Photograph above Navigation"
  title="Photograph above Navigation"
  >

Is there a better way to use named arguments? Any other suggestions?

Thanks,
Daniel



reply via email to

[Prev in Thread] Current Thread [Next in Thread]