
I gave this brief talk for the
Pittsburgh Perl Mongers on 2006-03-08. The talk is about
embedding domain-specific languages into Perl. The running
example is XHTML construction.
You can download the slides here:
pgh-pm-edsls.pdfMost of the ideas in the talk came from an earlier posting I made to the Perl Monks web site:
Embedding a mini-language for XML construction into Perl. I would like to thank
Aristotle, who contributed a nice syntax-simplification idea during the follow-up discussion.
The EDSL
The EDSL looks like this:
html {
head {
title { "My grand document!" }
};
body {
h1 { "Heading" };
p {
class_ "first"; # attribute class="first"
text "This is the first paragraph!";
style_ "font: bold"; # another attr
};
# it's just Perl, so we can mix in other code
for (2..5) {
p { "Plus paragraph number $_." }
}
};
};The embedded language renders into XHTML:
<html>
<head>
<title>My grand document!</title>
</head>
<body>
<h1>Heading</h1>
<p class="first" style="font: bold">This is the first paragraph!</p>
<p>Plus paragraph number 2.</p>
<p>Plus paragraph number 3.</p>
<p>Plus paragraph number 4.</p>
<p>Plus paragraph number 5.</p>
</body>
</html>