This blog is also available on Gemini (What's Gemini?)

For Mustache Templates in Perl, use Text::Caml


I like Mustache templates[1]. Unfortunately, the obvious Mustache parser in Perl (Template::Mustache)[2] is under-documented, over-documented, and not worth documenting. It has little example code and doesn't make it at all clear that you basically have to subclass it for any real use.

Fortunately, this problem has been solved in Text::Caml[3]. My one issue is that it's too sensitive to whitespace. It's good to give your eyes a resting place for sections and partials, like this:

{{# section}} {{> partial}} {{/ section}}

But Text::Caml will consider the leading whitespace as part of the name. Looking over the main Mustache docs, none of the examples show leading whitespace, and it's never specified what to do with whitespace. So I guess Text::Caml isn't *technically* wrong. I just don't like it this way.

Other than that, it's great.

Edit: Turns out the Mustache spec does say that partials and sections should be insensitive to whitespace:[4][5] (starting line 104)[6][7] (starting line 252)

Edit 2: I reported the whitespace issue[8] to the author's github issue tracker, expecting it to be a fairly low priority. He ended up fixing it the next day :)