Created
January 19, 2015 21:10
-
-
Save peczenyj/ea63c24145a0284b12fe to your computer and use it in GitHub Desktop.
generate dynamic html with perl
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; | |
use strict; | |
use warnings; | |
use feature 'say'; | |
sub import { | |
my $self = shift; | |
my ($callpack, $callfile, $callline) = caller; | |
foreach my $tag (@_) { | |
no strict 'refs'; | |
*{"${callpack}::${tag}"} = sub (;&%) { | |
my ( $main, %attrs ) = @_; | |
my $attrs = ""; | |
if(scalar keys %attrs){ | |
$attrs = " " . join " ", map { "${_}=\"$attrs{$_}\"" } keys %attrs; | |
} | |
if( $main ) { | |
say "<${tag}${attrs}>"; | |
my $r = $main->(); | |
say $r if $r; | |
say "</${tag}>"; | |
} else { | |
say "<${tag}${attrs}/>" | |
} | |
undef | |
} | |
} | |
} | |
1; |
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
<html> | |
<head> | |
<title> | |
hello world | |
</title> | |
</head> | |
<body> | |
<h1> | |
heading 1 | |
</h1> | |
<br/> | |
<a rel="nofollow" href="google.com"> | |
click here | |
</a> | |
</body> | |
</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
use strict; | |
use warnings; | |
use feature 'say'; | |
use HTML qw(html head title body h1 br a); | |
html { | |
head { | |
title { "hello world" }; | |
}; | |
body { | |
h1 { "heading 1" }; | |
br; | |
a { "click here" } href => 'google.com', rel => 'nofollow'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment