in reply to Embedding a mini-language for XML construction into Perl
For example, the following fragment:
can be simplified to this:html { head { title { text "Title" } }; body { p { class_ "warning"; text "paragraph" } } };
html { head { title {} "Title" }; body { p { class_ "warning" } "paragraph" } };
To effect the new syntax rules, we need only change the _elem and define_vocabulary functions from our original implementation. The changes are simple and marked with a hash-bang (#!):
sub _elem { my ($elem_name, $content_fn, $text) = @_; #! added $text arg # an element is represented by the triple [name, attrs, children] my $elem = [$elem_name, undef, undef]; do { local $__frag = $elem; $content_fn->(); text($text) if defined $text; #! new line }; push @{$__frag->[2]}, $elem; } sub define_vocabulary { my ($elems, $attrs) = @_; eval "sub $_(&@) { _elem('$_',\@_) }" for @$elems; #! proto eval "sub ${_}_(\$) { _attr('$_',\@_) }" for @$attrs; }
Can you spot any other syntax-reduction opportunities?
Cheers,
Tom
Tom Moertel : Blog / Talks / CPAN / LectroTest / PXSL / Coffee / Movie Rating Decoder
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simplifying the syntax further
by Aristotle (Chancellor) on Nov 22, 2005 at 11:32 UTC | |
by tmoertel (Chaplain) on Nov 22, 2005 at 15:59 UTC | |
by TimToady (Parson) on Nov 22, 2005 at 16:52 UTC | |
by Aristotle (Chancellor) on Nov 22, 2005 at 16:48 UTC |