Help language development. Donate to The Perl Foundation

HTML::BoreDOM cpan:ALLSOPP last updated on 2020-02-10


Build Status


Write HTML programmatically:

> use HTML::BoreDOM;

> put
> h("html",
>  h("body",
>   h("h1", "Hello, World!"),
>   h("p", "Lorem ipsum...")));

<h1>Hello, World!</h1>
<p>Lorem ipsum...</p>

The h() subroutine is imported automatically and returns an object (that stringifies to escaped HTML):

> h("span", "foo"); => "span", attrs => ${}, children => $["foo"])

> ~h("span", "foo");

HTML attributes are optionally declared using named arguments:

> ~h("a", :href<>, :target<_blank>, "Click Here!")
<a href="" target="_blank">Click Here!</a>

Looping is easy:

> my @items = <foo bar baz>;

> ~h("ul", 
>{ h("li", $_) }));


Templates are just functions:

> sub foo ($title, $subtitle, $content) {
>   ~ h("h1", $title)
>   ~ h("h2", $subtitle)
>   ~ h("p", $content)
> }

> foo("My Website", "Using templates", "Lorem ipsum ...");

<h1>My Website</h1>
<h2>Using templates</h2>
<p>Lorem ipsum ...</p>


Inline <script> and <style> won't work because certain characters (i.e. &, <, and >) are automatically escaped. The fix is trivial but has intentionally been left unimplemented keep this package simple.

Copyright (c) 2019, Owen Allsopp

This package is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.

See also

This idea is based on HyperScript.

Mixing logic and presentation is usually a Very Bad Idea.

Some alternatives with more features: