good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Which internal DSL are there in Perl? (Domain Specific Languages - Part 1)by LanX (Saint) |
on Aug 03, 2017 at 02:08 UTC ( [id://1196599]=perlmeditation: print w/replies, xml ) | Need Help?? |
Taking some definitions from Wikipedia and Martin Fowlers Book:
(emphasizes added)
DSLs are small languages, focused on a particular aspect of a software system. You can't build a whole program with a DSL, but you often use multiple DSLs in a system mainly written in a general purpose language. Now there are some more frequently used examples in Perl which come to mind like SQL and Template languages like in Template::Toolkit2
Both qualify as are for sure as DSLs. They are sub-languages which can be used by "domain experts" without any knowledge of Perl. And they are external, because neither SQL nor TT commands are implemented as Perl functions or follow Perl syntax.
Well designed traditional modules and classes are sometimes close to represent a sub-language but they still come with a lot of boilerplate. For instance an expert in the domain of TCL/TK may (probably?) not care much if the "host" language is Perl or Python or Ruby as long as he can call the commonly known methods. (see http://www.tkdocs.com/ for comparison of snippets in various languages). But the necessity to constantly repeat an $obj-> or PACK::AGE:: on the left hand side of a command doesn't make it a completely "fluent" approach. (On a side note: avoiding to repeat this LHS for method calls seems to be the principal mechanism in Ruby for transforming a simple class to what Rubyists like to call a "DSL", a bunch of function calls inside a block are somehow interpreted as method calls on the same object, hence very similar to the with(obj) {} mechanism in JavaScript - please correct me if not) Now, what are examples for internal DSLs in Perl? The only widely known one which comes to my mind is CGI::HTML::Functions which used to be part of CGI
For example, here's one way to make an ordered list:This example will result in HTML output that looks like this: Nice but IMHO it could be even more fluent. But are there others? There is for instance Rex which I suppose was inspired by Ruby's Puppet
And I think I could theoretically implement most Assembler languages as Perl code just with some minor changes. I just need to implement ASM-commands as uppercase subs, quote literals and adjust the comment symbol. like (original example) But there must be more examples of internal DSLs in Perl ... Could you please help me to find and classify them?
Cheers Rolf
Back to
Meditations
|
|