But I like pure functions. This approach leaves me no easy way to stick the generated HTML into a variable.

There's that print again. I said I was breaking those laws :)

our $print = sub { print shift }; sub table (&) { $print->("<table>\n"); shift->(); $print->("</table>\n +"); } sub row (&) { $print->("<tr>"); shift->(); $print->("</tr>\n"); } sub cell { $print->("<td>$_</td>") for @_; } table { row { cell "foo" } }; my $html = "<html><body>"; local $print = sub { $html .= shift }; table { row { cell "foo" } }; $html .= "</body></html>";
But really, I did not intend to write re-usable or pure code. Just something that feels comfortable and is very easy to use. In practice, I rarely need HTML in a variable. I don't mind writing HTML manually for that one time per month :)

Alternatively, break the rules again and just open the scalar as if it were a file:

sub table (&) { print-"<table>\n"; shift->(); print "</table>\n"; sub row (&) { print "<tr>"; shift->(); print "</tr>\n"; } sub cell { print "<td>$_</td>" for @_; } table { row { cell "foo" } }; my $html = "<html><body>"; open my $fh, '>>', \$html; select $fh; table { row { cell "foo" } }; close $fh; $html .= "</body></html>";

I cannot see how to do nested tables at all, when the table function is written to print.

You shouldn't need nested tables. If you find yourself needing a table in another table, that is probably because one table is there just for the layout of the page. And such a table isn't created with something like this, because you need attributes badly.

I meant the code to be simple. To get there, I broke quite some unwritten rules about purity and reusability. It was not my intention to support anything more than printing simple tables.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


In reply to Re: Re: Readable HTML tables by Juerd
in thread Readable HTML tables by Juerd

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.