Skip to content

Instantly share code, notes, and snippets.

@peczenyj
Created January 19, 2015 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peczenyj/ea63c24145a0284b12fe to your computer and use it in GitHub Desktop.
Save peczenyj/ea63c24145a0284b12fe to your computer and use it in GitHub Desktop.
generate dynamic html with perl
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;
<html>
<head>
<title>
hello world
</title>
</head>
<body>
<h1>
heading 1
</h1>
<br/>
<a rel="nofollow" href="google.com">
click here
</a>
</body>
</html>
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