4 ms·
POSIX M4 is one of those things I really want to be good for production, but whenever I've tried using it in production it's been insufficient for anything even
by kqr 4y ago
POSIX M4 is one of those things I really want to be good for production, but whenever I've tried using it in production it's been insufficient for anything even moderately complicated. (It's not that things are impossible, only that it's more maintainable long term to replace the m4 invocation with a Perl script or a more potent templater like Jinja2 in the cases where the macro expansion needs are large.)
The only place where I tried using M4 in production and I know it's still used there, the final system ended up generating the M4 templates and the script to invoke it from another script, and then yet another script fixes up some loose ends left by M4. Effectively the m4 step is entirely redundant and was only left in there because the people picking up the project after me didn't want to remove Chesterton's fence. (I learned later.)
Maybe there's a more useful non-POSIX M4, but I really like the idea of a POSIX standard templating/macro-expansion language. Only M4 isn't it, in my experience.
- usgroup 4y agoI think that M4 can be fine in production. Many years ago, I had used it to re-write a massive Squid Proxy configuration file which was mostly a collection of repeating blocks. The file was so big that no-one really understood how it worked, and it evolved by constant monkey patching. Post M4 and distilled to just its non-repeating elements, it was an order of magnitude smaller but also clearer because the macros designated the patterns and what wasn't in a macro was actually unique.
- cbsmith 4y agom4 is fine in production if what you're doing really is just string manipulation. It just tends to be used in cases like the one in this article, where you are manipulating structured text. In that context, you run the risk of errors/complexity/fragility because you are manipulating strings rather the structures.
- cbsmith 4y ago"rather than structures"
- bonzini 4y agoAutotools have a library of m4 macros called m4sugar that is quite powerful. I can't quite recall whether it's limited to GNU M4 and as to the readability, well, it's still m4... https://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Programming-in-M4sugar.html https://www.gnu.org/software/autoconf/manual/autoconf-2.67/h...
- Karrot_Kream 4y agoI chose M4 for $WORK to use in a context where we had config files with templated environment values and we didn't want to depend on a programming language for those templates. Since the configs were created and placed on container start, M4 came with the base image and was easy to use just to set a few values in the template that depended on the container environment. The whole thing works with having a "template file" which looks mostly like the config with a couple M4 macros to source envars. Envar sourcing logic came from a base M4 file we included in each template at the top. There were a couple sharp edges with escaping but the obviousness of the whole setup made M4 fine. If M4 was a bit easier to work with I'd love to use it for something more complicated, but for our purposes it was fine.