- 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... ;-)
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |