Created
May 4, 2015 18:46
-
-
Save jeffa/d51f8047695d8a134b58 to your computer and use it in GitHub Desktop.
Just another way to spit out HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package HTML::Auto; | |
| use strict; | |
| use warnings; | |
| use Exporter 'import'; | |
| our @EXPORT = qw( html head title body h1 p br table Tr td ); | |
| our $AUTOLOAD; | |
| sub new { bless {}, shift } | |
| sub AUTOLOAD { | |
| my $self = ref($_[0]) eq __PACKAGE__ ? shift : undef; | |
| my $attr = ref($_[0]) eq 'HASH' ? shift : {}; | |
| my $tag = $AUTOLOAD =~ s/.*:://r; | |
| my $attr_txt = ''; | |
| $attr_txt .= qq( $_="$attr->{$_}") for keys %$attr; | |
| return @_ ? "<$tag$attr_txt>@_</$tag>" : "<$tag$attr_txt />"; | |
| } | |
| 1; | |
| __DATA__ | |
| use HTML '!DEFAULT'; | |
| print '<!DOCTYPE html>', | |
| html( | |
| head( | |
| title("some html") | |
| ), | |
| body( | |
| h1( "some headline" ), | |
| p( {class => 'foo'}, "yadda yadda yadda", br(), "more yadda" ), | |
| p( {class => 'bar'}, "yadda yadda yadda" ), | |
| table( {summary => 'table', border => 1}, | |
| Tr( map td($_), 'a' .. 'f' ), | |
| Tr( map td($_), 'g' .. 'l' ), | |
| Tr( map td($_), 'm' .. 'r' ), | |
| Tr( map td($_), 's' .. 'x' ), | |
| ), | |
| ), | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment