5 HTML::AsSubs - functions that construct a HTML syntax tree
11 h1("This is the heading"),
12 p("This is the first paragraph which contains a ",
13 a({href=>'link.html'}, "link"),
15 img({src=>'img.gif', alt=>'image'}),
23 This module exports functions that can be used to construct various
24 HTML elements. The functions are named after the tags of the
25 correponding HTML element and are all written in lower case. If the
26 first argument is a hash reference then it will be used to initialize the
27 attributes of this element. The remaining arguments are regarded as
30 For a similar idea (i.e., it's another case where the syntax tree
31 of the Perl source mirrors the syntax tree of the HTML produced),
32 see HTML::Element's C<new_from_lol> method.
34 For what I now think is a cleaner implementation of this same idea,
35 see the excellent module C<XML::Generator>, which is what I suggest
36 for actual real-life use. (I suggest this over C<HTML::AsSubs> and
37 over C<CGI.pm>'s HTML-making functions.)
39 =head1 ACKNOWLEDGEMENT
41 This module was inspired by the following message:
43 Date: Tue, 4 Oct 1994 16:11:30 +0100
44 Subject: Wow! I have a large lightbulb above my head!
46 Take a moment to consider these lines:
48 %OVERLOAD=( '""' => sub { join("", @{$_[0]}) } );
50 sub html { my($type)=shift; bless ["<$type>", @_, "</$type>"]; }
52 :-) I *love* Perl 5! Thankyou Larry and Ilya.
57 p.s. If you didn't get it, think about recursive data types: html(html())
58 p.p.s. I'll turn this into a much more practical example in a day or two.
59 p.p.p.s. It's a pity that overloads are not inherited. Is this a bug?
63 The exported link() function overrides the builtin link() function.
64 The exported tr() function must be called using &tr(...) syntax
65 because it clashes with the builtin tr/../../ operator.
69 L<HTML::Element>, L<XML::Generator>
74 use vars qw(@ISA $VERSION @EXPORT);
76 require HTML::Element;
82 # Problem: exports so damned much. Has no concept of "export only HTML4
83 # elements". TODO:?? make something that make functions that just
84 # wrap XML::Generator calls?
88 head title base link meta isindex nextid script style
89 body h1 h2 h3 h4 h5 h6 p pre div blockquote
93 dfn cite code em kbd samp strong var address
95 center font big small strike
97 table tr td th caption
98 form input select option textarea
101 frame frameset noframe
106 $code = "sub $_ { _elem('$_', \@_); }\n" ;
114 =head1 Private Functions
118 The _elem() function is wrapped by all the html 'tag' functions. It
119 takes a tag-name, optional hashref of attributes and a list of content
128 if (@_ and defined $_[0] and ref($_[0]) eq "HASH") {
131 my $elem = HTML::Element->new( $tag, %$attributes );
132 $elem->push_content(@_);