• performance: One needs to reduce runtime as much as possible, like by memoization

There's another package which implements the same DSL: Template::Declare. I benchmarked these two...

#!/usr/bin/perl use Benchmark qw(cmpthese); use HTML::Writer qw(xhtml1-transitional.dtd); package Writer { sub writer { use HTML::Writer; my $str = render { HTML { HEAD { TITLE { "foo bar"}; }; BODY { class_ "ugly"; onload_ "javascript: mumble()"; DIV { class_ "foo"; id_ "bar"; t "If in doubt, mumble."; IMG { src_ "foo.jpg" }; }; TABLE { my $c; for ($foo, $bar, baz) { TR { TD { $_ }; TD { $c++}; } } }; DIV { class_ "bar"; t "End of that." }; A { href_ "http://perlmonks.org"; t "Perl Monks Website"; name_ "PerlMonks"; title_ "PM"; }; } }; } 1; } } no HTML::Writer; package Template { use Template::Declare::Tags; use base 'Template::Declare'; template simple => sub { html { head { title { "foo bar"}; }; body { attr { class => "ugly" }; attr { onload => "javascript: mumble()" }; div { attr { class => "foo"}; attr { id => "bar"}; "If in doubt, mumble."; img { src is "foo.jpg" }; }; table { my $c; for ($foo, $bar, baz) { row { cell { $_ }; cell { $c++}; } } }; div { attr { class => "bar"};"End of that." }; a { href is "http://perlmonks.org"; "Perl Monks Website"; attr { name => "PerlMonks"}; attr { title => "PM"}; }; }; }; }; }; package main; use Template::Declare; Template::Declare->init( dispatch_to => ['Template'] ); sub template { my $str = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transition +al//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . +"\n" . Template::Declare->show( 'simple' ); } cmpthese(-1, { writer => \&Writer::writer, template => \&template, });

Result:

Rate template writer template 1659/s -- -20% writer 2073/s 25% --

Not too bad... update: the difference, I guess, is mainly due to that I'm just implementing a wheel - without a car around it. But the wheel that has to be implemented is a dsl pragma, which would switch on/off various DSLs at compile time, even nested - think of SGML/SVG/XML/XUL/JS and whatnot, and each would make their proper syntax available as plain perl inside their current scope.

  • namespace polution: The effects of the embedded language must be contained to a lexical scope.

To that end, my current code on github works as a pragma. The DSL routines are imported at compile time with use HTML::Writer, unimported with no HTML::Writer and handled via AUTOLOAD at runtime. Conflicting subroutines of the caller are saved and restored at no HTML::Writer, so there is no namespace pollution at all. Regarding inspection:

I already wrote code for this in my Macro module (github only).

Would you please provide the URL? I might learn something... ;-)

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re^5: Which internal DSL are there in Perl? (HTML::Writer as pragma) by shmem
in thread Which internal DSL are there in Perl? (Domain Specific Languages - Part 1) by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.