in reply to Re: Readable HTML tables
in thread Readable HTML tables

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' }

Replies are listed 'Best First'.
Re: Re: Re: Readable HTML tables
by Sidhekin (Priest) on Aug 26, 2003 at 08:14 UTC

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

    Yes, sure. I said "no easy way", not "no way at all". But you still cannot do nested elements.

    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.

    You may tell me what I "should", but when people are paying me to put tables in tables, I prefer not to argue. Besides, I addressed my need of attributes, right? :-)

    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.

    Acknowledged and appreciated. I am not saying "thou shalt not print", just I often find myself needing to stuff it into a variable. Or nested tables. I just took inspiration from it, to do my own thing. Personally, I never find myself writing "simple tables" (no one is paying for that), so to me the inspiring thing was the code layout, and I tried to work out how I could make my code look like that.

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"

      but when people are paying me (...), I prefer not to argue.

      Very good point :)

      I just took inspiration from it, to do my own thing.

      That's why I posted it here instead of CPAN. This is a pattern you can hack to fit your own needs. And IMHO, this is something that needs to be copied and pasted. Code re-use is fine, but not when the 3-line alternative provides all hackability one could ever wish for :)

      Your code, on the other hand, would be a very nice addition to CPAN. Please consider making it a public module.

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