4 ms·
I always remember m4 encounters from autoconf struggles of building things from source a long time ago (slackware 6). Back when I never cared about m4, Today,
by sigmonsays 7y ago
I always remember m4 encounters from autoconf struggles of building things from source a long time ago (slackware 6).
Back when I never cared about m4, Today, I find it interesting to understand what problem its trying to solve. I dont feel like its practical anymore but I think it is important to not discard use of old tools and understand them.
- jolmg 7y agohttps://en.wikipedia.org/wiki/M4_(computer_language)#History https://en.wikipedia.org/wiki/M4_(computer_language)#History I see it as a more powerful version of CPP. It might be somewhat based on it. CPP seems to have been developed in 1972-3[1], while M4 was developed in 1977. Today, I think we're using template systems like Mustache, Handlebars, ERB, Jinja, etc. where otherwise macro systems would be used. The difference between macro systems / preprocessors and templates is that the latter comes with syntax (like <%= %> for ERB) that always makes it clear what's handled by the template system. With M4 and CPP, it's implicit and instead you have syntax (`' or []) to exclude stuff from being handled by the macro system. I think that change is because, before, macro systems were meant to be used mainly with programming languages to avoid repetition. Then, programming languages became concise and more flexible, so they didn't need to be handled by another language like C has CPP. Our need changed to having to avoid repetition with data files like HTML. Perhaps having implicit expansion when used with an underlying programming language is not so much an issue, or at least it could be handled with a convention like how CPP variables and functions are normally capitalized. With data files, I suppose being able to differentiate between what's HTML and what's ERB was probably more important. [1] https://softwareengineering.stackexchange.com/questions/309389/what-is-the-origin-of-the-c-preprocessor https://softwareengineering.stackexchange.com/questions/3093...
- kd5bjo 7y agoThe pattern I’ve fallen into with m4 is to have a preamble that sets up whatever definitions I’m going to need, redefine the quotes “backwards”, and then place a copy of the new close quote on its own line to start the main body of the file. It then acts very similarly to how you describe templating languages, where you mark the parts that should be parsed instead of the parts that shouldn’t.
- throwaway_pdp09 7y agoI'm intrigued, could you give a small example please?
- kd5bjo 7y agoNot right now, unfortunately; my computer is at the office and I don’t use m4 enough to give an example from memory. I’ll try to remember next time I go in.
- jolmg 7y agoI imagine it's something like this? $ m4 << EOF changequote(],[)dnl define(foo,bar)dnl ]Lorem ipsum foo [foo] Lorem ipsum[ EOF Lorem ipsum foo bar Lorem ipsum
- kd5bjo 7y agoThat looks about right; I’m usually outputting formats that don’t care about blank lines and so skip the “dnl”s and put the start and end quotes on their own lines for readability.
- throwaway_pdp09 7y agoHeh. Never seen that before. I'll have to have a play. Thanks both.
- cryptonector 7y agoOh. Oh, that's just brilliant. (The example in the other subthread makes it clear.)