home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / HTML / AsSubs.pm next >
Text File  |  1997-11-06  |  2KB  |  108 lines

  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 I<hash> then it will be used to initialize the
  27. attributes of this element. The remaining arguments are regarded as
  28. content.
  29.  
  30. =head1 ACKNOWLEDGEMENT
  31.  
  32. This module was inspired by the following message:
  33.  
  34.  Date: Tue, 4 Oct 1994 16:11:30 +0100
  35.  Subject: Wow! I have a large lightbulb above my head!
  36.  
  37.  Take a moment to consider these lines:
  38.  
  39.  %OVERLOAD=( '""' => sub { join("", @{$_[0]}) } );
  40.  
  41.  sub html { my($type)=shift; bless ["<$type>", @_, "</$type>"]; }
  42.  
  43.  :-)  I *love* Perl 5!  Thankyou Larry and Ilya.
  44.  
  45.  Regards,
  46.  Tim Bunce.
  47.  
  48.  p.s. If you didn't get it, think about recursive data types: html(html())
  49.  p.p.s. I'll turn this into a much more practical example in a day or two.
  50.  p.p.p.s. It's a pity that overloads are not inherited. Is this a bug?
  51.  
  52. =head1 BUGS
  53.  
  54. The exported link() function overrides the builtin link() function.
  55. The exported tr() function must be called using &tr(...) syntax
  56. because it clashes with the builtin tr/../../ operator.
  57.  
  58.  
  59. =head1 SEE ALSO
  60.  
  61. L<HTML::Element>
  62.  
  63. =cut
  64.  
  65. require HTML::Element;
  66. require Exporter;
  67. @ISA = qw(Exporter);
  68.  
  69. @TAGS = qw(html
  70.        head title base link meta isindex nextid script style
  71.        body h1 h2 h3 h4 h5 h6 p pre div blockquote
  72.        a img br hr
  73.        ol ul dir menu li
  74.        dl dt dd
  75.        dfn cite code em kbd samp strong var address 
  76.        b i u tt
  77.            center font big small strike
  78.            sub sup
  79.        table tr td th caption
  80.        form input select option textarea
  81.            object applet param
  82.            map area
  83.            frame frameset noframe
  84.       );
  85.  
  86. for (@TAGS) {
  87.     push(@code, "sub $_ { _elem('$_', \@_); }\n");
  88.     push(@EXPORT, $_);
  89. }
  90. eval join('', @code);
  91. if ($@) {
  92.     die $@;
  93. }
  94.  
  95. sub _elem
  96. {
  97.     my $tag = shift;
  98.     my $attributes;
  99.     if (@_ and defined $_[0] and ref($_[0]) eq "HASH") {
  100.     $attributes = shift;
  101.     }
  102.     my $elem = new HTML::Element $tag, %$attributes;
  103.     $elem->push_content(@_);
  104.     $elem;
  105. }
  106.  
  107. 1;
  108.