1HTML::AsSubs(3) User Contributed Perl Documentation HTML::AsSubs(3)
2
3
4
6 HTML::AsSubs - functions that construct a HTML syntax tree
7
9 use HTML::AsSubs;
10 $h = body(
11 h1("This is the heading"),
12 p("This is the first paragraph which contains a ",
13 a({href=>'link.html'}, "link"),
14 " and an ",
15 img({src=>'img.gif', alt=>'image'}),
16 "."
17 ),
18 );
19 print $h->as_HTML;
20
22 This module exports functions that can be used to construct various
23 HTML elements. The functions are named after the tags of the
24 corresponding HTML element and are all written in lower case. If the
25 first argument is a hash reference then it will be used to initialize
26 the attributes of this element. The remaining arguments are regarded as
27 content.
28
29 For a similar idea (i.e., it's another case where the syntax tree of
30 the Perl source mirrors the syntax tree of the HTML produced), see
31 HTML::Element's "new_from_lol" method.
32
33 For what I now think is a cleaner implementation of this same idea, see
34 the excellent module "XML::Generator", which is what I suggest for
35 actual real-life use. (I suggest this over "HTML::AsSubs" and over
36 "CGI.pm"'s HTML-making functions.)
37
39 This module was inspired by the following message:
40
41 Date: Tue, 4 Oct 1994 16:11:30 +0100
42 Subject: Wow! I have a large lightbulb above my head!
43
44 Take a moment to consider these lines:
45
46 %OVERLOAD=( '""' => sub { join("", @{$_[0]}) } );
47
48 sub html { my($type)=shift; bless ["<$type>", @_, "</$type>"]; }
49
50 :-) I *love* Perl 5! Thankyou Larry and Ilya.
51
52 Regards,
53 Tim Bunce.
54
55 p.s. If you didn't get it, think about recursive data types: html(html())
56 p.p.s. I'll turn this into a much more practical example in a day or two.
57 p.p.p.s. It's a pity that overloads are not inherited. Is this a bug?
58
60 The exported link() function overrides the builtin link() function.
61 The exported tr() function must be called using &tr(...) syntax because
62 it clashes with the builtin tr/../../ operator.
63
65 HTML::Element, XML::Generator
66
67 html head title base link meta isindex nextid script style body h1 h2 h3 h4
68 h5 h6 p pre div blockquote a img br hr ol ul dir menu li dl dt dd dfn
69 cite code em kbd samp strong var address span b i u tt center font big
70 small strike sub sup table tr td th caption form input select option
71 textarea object applet param map area frame frameset noframe
72 A bunch of methods for creating tags.
73
75 _elem()
76 The _elem() function is wrapped by all the html 'tag' functions. It
77 takes a tag-name, optional hashref of attributes and a list of content
78 as parameters.
79
80
81
82perl v5.12.2 2010-12-20 HTML::AsSubs(3)