Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Simplifying the syntax further

by Aristotle (Chancellor)
on Nov 22, 2005 at 11:32 UTC ( [id://510731]=note: print w/replies, xml ) Need Help??


in reply to Simplifying the syntax further
in thread Embedding a mini-language for XML construction into Perl

That looks uglier and is less obvious. I’d prefer if it were possible to have the block’s value taken as its text content. This seemed tricky at first, because you don’t want to force users to put an explicit return;, undef;, ''; or whatever at the end of a block to avoid having the last expression of every block added as text content. After some reflection, however, it’s not tricky at all.

There are only two cases: either you have a complex element with multiple children, be they sub-elements or full-on mixed content; or you have an element with nothing but text in it. These are clearly distinguishable: if the element has nothing but text in it, it won’t have any children yet when the block returns; if the element has complex content, it will already have explicitly constructed children when the block returns.

The changes for this turn out even more trivial. Here’s the original _elem modified to meet this spec, with hashbangs:

sub _elem { my ( $elem_name, $content_fn ) = @_; # an element is represented by the triple [name, attrs, children] my $elem = [ $elem_name, undef, undef ]; my $ret = { local $__frag = $elem; $content_fn->(); }; #! keep ret +val push @{ $elem[2] }, $ret if defined $ret and not @{ $elem[2] }; #! + new line push @{ $__frag->[2] }, $elem; }

The only case where you get strange behaviour is when the block for an empty element contains code, something like br { ++$breaks } – this would now have to be written as br { ++$breaks; undef }. But you can now say

html { head { title { "Lorem ipsum" } }; body { # ... }; }

If you use text or if you construct any other element explicitly, the block’s return value will not interfere.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Simplifying the syntax further
by tmoertel (Chaplain) on Nov 22, 2005 at 15:59 UTC
    Good contribution! Your simplified text syntax is prettier and more intuitive.

    The corner case where an empty element contains code is a small blemish and an easy price to pay for the benefits of the simplified text syntax. Maybe we can even reduce the blemish by introducing another helper that declares a block to represent an empty content model:

    sub empty(&) { shift->(); undef }
    Then the corner-case becomes:
    doc { br { empty { ++$breaks } } }
    It's still not perfect, but maybe we can think of yet another improvement.

    Cheers,
    Tom

      Well, there's always:
      doc { br { ++$breaks; () } }
      which pretty much says "empty" to me. But maybe it's better to document it out front. I wonder if something resembling:
      doc { br {} (++$breaks) }
      could be made to work with an additional parameter that is ignored.

      That doesn’t look like a useful addition. The user still has to understand the edge case and know when to apply empty manually, but the cost of learning new syntax for it is not amortised, since it offers nothing over saying undef; explicitly.

      For the case of br and hr, the better solution would be to declare them as empty elements. (Then you could also warn/die if the user accidentally does call text or an element constructor.)

      Of course that still won’t help when the user wants the occasional empty td. I see no way to handle that without manual intervention, though, so there might as well be no magic for it at all. Magic must be transparent to earn the name; if it’s not, it just adds extra cost.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://510731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-18 00:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found