Debian lenny version packages
[pkg-perl] / deb-src / libhtml-tree-perl / libhtml-tree-perl-3.23 / lib / HTML / AsSubs.pm
1 package HTML::AsSubs;
2
3 =head1 NAME
4
5 HTML::AsSubs - functions that construct a HTML syntax tree
6
7 =head1 SYNOPSIS
8
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
21 =head1 DESCRIPTION
22
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
28 content.
29
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.
33
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.)
38
39 =head1 ACKNOWLEDGEMENT
40
41 This module was inspired by the following message:
42
43  Date: Tue, 4 Oct 1994 16:11:30 +0100
44  Subject: Wow! I have a large lightbulb above my head!
45
46  Take a moment to consider these lines:
47
48  %OVERLOAD=( '""' => sub { join("", @{$_[0]}) } );
49
50  sub html { my($type)=shift; bless ["<$type>", @_, "</$type>"]; }
51
52  :-)  I *love* Perl 5!  Thankyou Larry and Ilya.
53
54  Regards,
55  Tim Bunce.
56
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?
60
61 =head1 BUGS
62
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.
66
67 =head1 SEE ALSO
68
69 L<HTML::Element>, L<XML::Generator>
70
71 =cut
72
73 use strict;
74 use vars qw(@ISA $VERSION @EXPORT);
75
76 require HTML::Element;
77 require Exporter;
78 @ISA = qw(Exporter);
79
80 $VERSION = '1.16';
81
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?
85
86 use vars qw(@TAGS);
87 @TAGS = qw(html
88            head title base link meta isindex nextid script style
89            body h1 h2 h3 h4 h5 h6 p pre div blockquote
90            a img br hr
91            ol ul dir menu li
92            dl dt dd
93            dfn cite code em kbd samp strong var address 
94            b i u tt
95            center font big small strike
96            sub sup
97            table tr td th caption
98            form input select option textarea
99            object applet param
100            map area
101            frame frameset noframe
102           );
103
104 for (@TAGS) {
105         my $code;
106         $code = "sub $_ { _elem('$_', \@_); }\n" ;
107         push(@EXPORT, $_);
108         eval $code;
109         if ($@) {
110                 die $@;
111         }
112 }
113
114 =head1 Private Functions
115
116 =head2 _elem()
117
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
120 as parameters.
121
122 =cut
123
124 sub _elem
125 {
126     my $tag = shift;
127     my $attributes;
128     if (@_ and defined $_[0] and ref($_[0]) eq "HASH") {
129         $attributes = shift;
130     }
131     my $elem = HTML::Element->new( $tag, %$attributes );
132     $elem->push_content(@_);
133     $elem;
134 }
135
136 1;