4 ms·
Most languages will have literals that can be used to easily represent HTML data: [a => {href=>url, name=>'next'}, 'Go to the', [i => 'next'], 'element'];
by draegtun 14y ago
Most languages will have literals that can be used to easily represent HTML data:
[a => {href=>url, name=>'next'}, 'Go to the', [i => 'next'], 'element']; # perl5
[a => {:href(url), :name<next>}, 'Go to the', [:i<next>], 'element']; # perl6
[:a => {:href=>url, :name=>'next'}, 'Go to the', [:i => 'next'], 'element'] # ruby
Your Lua example is very similar to the HTML::AsSubs CPAN module (https://metacpan.org/module/HTML::AsSubs https://metacpan.org/module/HTML::AsSubs). Here is your example using this module:
use HTML::AsSubs;
sub url { 'http://www.anythingfornow.com' }
my $h = html(
a( {href=>url, name=>'next'},
'Go to the',
i('next'),
'element',
),
);
say $h->as_HTML;
- james2vegas 14y agoWhich is, of course, just CGI.pm's html functions again.
- chalst 14y agoa { ... } and i "next" are not table elements, they are function applications. All of your examples use more magic characters than the Lua example I gave.
- draegtun 14y agoThe HTML::AsSubs example uses functions and works exactly (see [1]) the same way your Lua code does. The first set of examples are just to show that you can model HTML data within common Hash/Array literals available in most languages. re: magic characters - The perl6 example does use some extra shortcut niceties :) However the perl5 example will actually work unchanged in perl6. The Ruby example is only slightly different to perl5 because it uses symbol sigil (Perl like Lua allows barewords on LHS of a table/hash key assignment). [1] - The only difference is the Lua a() function receives all its content via a single Table. Whereas HTML::AsSubs a() is a variadic function and uses the Hash literal {} for assigning HTML attributes.