ianmacd seems to
think Ruby's missing_method is a unique feature. I think
it's more likely it was borrowed from Perl:
#!/usr/bin/perl
sub main::AUTOLOAD {
my ($method, $data, %attrs) = ($AUTOLOAD, @_);
my ($tag) = $method =~ /.*::(\w+)/g;
my $attr_str;
foreach my $key (keys %attrs) {
$attr_str .= sprintf (" %s='%s'", $attrs{$key});
}
return sprintf ("<%s%s>%s</%s>",
$tag, $attr_str, $data, $tag);
}
print &a ('Google', href => 'http://www.google.com'), "\n";
print &ul(&li('item1') . &li('item2') . &li('item3')), "\n";
Of course, TMTOWTDI, and ianmacd already
mentions CGI.pm, which does the same thing, only
more robustly.
Another module is quite useful if you want to edit your
html tree at some point after creating it,
HTML::Element. An example of how to create a tree
with it, and print it:
use HTML::Element;
my $foo = HTML::Element->new_from_lol (
['p',
['a', {href => 'http://www.google.com'},
'Google'
],
['ul',
map ['li', $_], qw(item1 item2 item3)
],
],
);
print $foo->as_HTML;