stuff
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Emacs</title>
|
||||
<meta charset="UTF-8"/>
|
||||
|
||||
132
posts/html-composer.html
Normal file
132
posts/html-composer.html
Normal file
@@ -0,0 +1,132 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>HTML::Composer and Perl HTML templating</title>
|
||||
<meta content="Description of post" name="description">
|
||||
<meta name="robots">
|
||||
<meta content="Awesome keywords" name="keywords">
|
||||
<meta charset="utf-8" content="utf-8" name="charset">
|
||||
<link href="/index.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/default.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<a href="/"><< back</a>
|
||||
<h1>Perl HTML templating</h1>
|
||||
<div><small>June 25th, 2026</small></div>
|
||||
<p>
|
||||
Perl has some great templating libraries for HTML. Most of them are pretty fast, and do what you want.
|
||||
<a href="https://metacpan.org/pod/Template::Toolkit">Template::Toolkit</a> is sort of the "gold standard", and you see it most
|
||||
often in the wild. <a href="https://metacpan.org/pod/Mojo::Template">Mojo::Template</a> is the "newer" kid on the block, and is most
|
||||
associated with the <a href="https://metacpan.org/pod/Mojolicious">Mojolicious</a> framework/toolkit, if you're writing a Mojolicious application
|
||||
you're likely using Mojo::Template. There are also a few niche templating tools, like <a href="https://metacpan.org/pod/Text::Xslate">Text::Xslate</a> (unfortunately, pretty much dead), and
|
||||
<a href="https://metacpan.org/pod/Template::Tiny">Template::Tiny</a> (supported, but not seen very often). Like I said earlier, all of these libraries are great,
|
||||
you write templated text, and they give you HTML back, awesome. However, they all share a common "problem", they make me write HTML. I'm a Perl developer,
|
||||
I like writing Perl, I want to write Perl, so when I have to switch contexts to HTML it annoys me. To compound this problem, I just got off a project that used a tool called
|
||||
<a href="https://github.com/weavejester/hiccup">hiccup</a>, hiccup let me write code in its native toungue (Clojure), and get back HTML in the form of a string. This rocked!
|
||||
Not only did I not have to write HTML, it was actually <i>intuitive</i>, it provided me with a DSL for HTML. So I took this idea, and ported it to Perl, in the form of
|
||||
<a href="https://metacpan.org/pod/HTML::Composer">HTML::Composer</a>. HTML::Composer is a library that does its best to emulate that sort of intuitiveness, and to my surprise,
|
||||
it seems to.
|
||||
</p>
|
||||
<h2>HTML::Composer</h2>
|
||||
<p>
|
||||
HTML::Composer only outputs HTML. It is a Perl data-structure to HTML converter, it takes an ARRAY ref, and outputs a string:
|
||||
</p>
|
||||
<pre><code class="language-perl">
|
||||
use HTML::Composer;
|
||||
|
||||
my $h = HTML::Composer->new();
|
||||
my $html = $h->html([
|
||||
head => [
|
||||
title => ["My Site"],
|
||||
script => {
|
||||
src => "/js/myScript.js",
|
||||
type => "text/javascript"
|
||||
}
|
||||
],
|
||||
body => [
|
||||
h1 => ["Hello World!"],
|
||||
br => {},
|
||||
div => { class => [ "p-3", "background-red" ] } => [
|
||||
"Hello World!", h2 => ["Test 123"]
|
||||
]
|
||||
]
|
||||
]
|
||||
);
|
||||
say $html; # <!DOCTYPE html><html> ...
|
||||
</code></pre>
|
||||
<p>
|
||||
You can also render partial HTML:
|
||||
</p>
|
||||
<pre><code class="language-perl">
|
||||
my $h = HTML::Composer->new;
|
||||
my $html = $h->partial([
|
||||
div => [
|
||||
"Hello, World!",
|
||||
a => { href => "https://www.google.com" } => ["www.google.com"]
|
||||
]
|
||||
]);
|
||||
|
||||
say $html;
|
||||
</code></pre>
|
||||
<p>
|
||||
Since HTML::Composer operates inside of Perl data structures, and doesn't need to parse HTML, it's quite fast. Faster than any other HTML templating library I could find.
|
||||
The following benchmark is the average of five different one-hundred-million iteration runs rendering a medium sized HTML page that changed between iterations,
|
||||
with the caveat being, each of these libraries was expected to take an input scalar and output to a scalar variable (Text::Xslate is optimized to read from a file not a string, so it hated this benchmark):
|
||||
</p>
|
||||
<pre><code>
|
||||
Rate Text::Xslate Mojo::Template Template::Tiny Template::Toolkit HTML::Composer
|
||||
Text::Xslate 433/s -- -78% -93% -93% -94%
|
||||
Mojo::Template 1969/s 354% -- -69% -70% -72%
|
||||
Template::Tiny 6410/s 1379% 226% -- -1% -9%
|
||||
Template::Toolkit 6494/s 1398% 230% 1% -- -8%
|
||||
HTML::Composer 7042/s 1525% 258% 10% 8% --
|
||||
</code></pre>
|
||||
<p>
|
||||
And, while being very quick, it also performs basic HTML validations to ensure you're sending something proper (it does not make sure the tags you pass are correct, this is very expensive).
|
||||
Instead of spending time parsing and building the HTML tree, the developer has already done the work, so all we need to do is <i>compose</i> it to HTML. It also performs the necessary escapes by default, if you need to avoid escaping,
|
||||
you can use the unsafe method <pre><code class="language-perl">[ div => $h->unsafe("Stuff that shouldn't be escaped!"); ]</code></pre>
|
||||
</p>
|
||||
<h3>How it works</h3>
|
||||
<p>
|
||||
HTML::Composer uses a depth-first search, with a stack to traverse the HTML array you provide,
|
||||
based on the ordering of the data it makes assumptions about what you want, a string followed by a hash means you want to
|
||||
write a tag with some attributes, if that is then followed by an array, a tag with attributes and children. We verify if the tag
|
||||
is allowed to have children, then, we insert a reference to the current tag's scalar after the hash, and array. If a tag has no children, ie a tag with just
|
||||
a hash following, then we can finish that tag right now by pushing the HTML string output to the output array. When we encounter an array,
|
||||
simply push all of its elements onto the stack. Rinse and repeat until we encounter a scalar reference, and close the tag.
|
||||
</p>
|
||||
<h3>Optimizations + learnings</h3>
|
||||
<p>
|
||||
I learned Perl string concatenation is really slow. In the first pass at this project I used a <pre><code class="language-perl">$root</code></pre> scalar to be the final output,
|
||||
and concatenated the HTML in place, however after doing some r&d, I realised that pushing to an array, and then using Perl's <pre><code class="language-perl">join('', @array)</code></pre>
|
||||
function, was almost one order of magnitude faster. The reason this is faster, is Perl's <a href="https://github.com/Perl/perl5/blob/909c414e5450fe64ba145e6a3f3ed2e97999f060/doop.c#L666">do_join</a> function
|
||||
is highly optimized, and seems to allocate the size of string it thinks it needs ahead of time, which saves a lot of CPU time.
|
||||
</p>
|
||||
<p>
|
||||
The main logic tree uses ref and string eq's, I attempted to swap this to using regexp with a prefix <pre><code class="language-perl">ref($foo) =~ /^SC/ # SCALAR</code></pre>, this is typically how I've optimized
|
||||
logic in C code in the past, though, instead of regexp its referencing the bytes directly, which in retrospect is obviously going to be much faster than this.
|
||||
However I did expect the prefix check with regexp to be at least slightly faster, but alas it's quite a bit slower.
|
||||
</p>
|
||||
<h2>Conclusion</h2>
|
||||
<p>
|
||||
Perl has great HTML templating libraries. I hate writing HTML so I wrote one that lets me stay in Perl, and export templates via sub-routines.
|
||||
It performs really well, which is to be expected, as it does no parsing of any actual text. I hope someone else with similar pains will try it out at some point.
|
||||
Thanks for reading!
|
||||
</p>
|
||||
<h4>Related links:</h4>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://codeberg.org/rawleyfowler/perl-HTML-Composer">HTML::Composer source code</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://metacpan.org/pod/HTML::Composer">HTML::Composer</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p>
|
||||
Footnote: yes your eyes do not decieve you, that is codeberg, I'm going to be migrating my work off Github over the next couple weeks.
|
||||
AI was not involved in this project at all.
|
||||
</p>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script>
|
||||
<script>hljs.highlightAll();</script>
|
||||
</body>
|
||||
</html>
|
||||
20
posts/http2-and-perl.html
Normal file
20
posts/http2-and-perl.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>2024</title>
|
||||
<meta name="description" content="Plans for 2024" />
|
||||
<link rel="stylesheet" href="/index.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<a href="/"><< back</a>
|
||||
<h1>Perl and HTTP/2</h1>
|
||||
<p>
|
||||
Perl is a great language for building applications that interface over HTTP. It has some of the best tooling for
|
||||
database access, sending/receiving HTTP requests:
|
||||
<a href="https://metacpan.org/pod/LWP">LWP</a>, <a href="https://metacpan.org/pod/Plack">Plack</a> and <a href="https://metacpan.org/pod/Mojolicious">Mojolicious</a>.
|
||||
The only glaring weakness to a Perl application as it stands, is typically, Perl servers only support HTTP/1.1. 1.1 is a "fine"
|
||||
protocol, especially for its time. However, in 2026, it's starting to show it's age. HTTP/2 isn't just "faster", it's much more efficient with your server's resources.
|
||||
It uses a single TCP socket also called a "stream" for client's to use, instead of opening multiple sockets per page load.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
20
posts/podman-rootless-on-alpine-linux.html
Normal file
20
posts/podman-rootless-on-alpine-linux.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Rootless Podman on Alpine Linux</title>
|
||||
<meta name="description" value="My experience setting up rootless podman on alpine linux">
|
||||
<meta name="keywords" value="Alpine Linux Podman OpenRC Rootless">
|
||||
<link rel="stylesheet" href="/index.css" />
|
||||
</head>
|
||||
<body>
|
||||
<a href="/"><< back</a>
|
||||
<p>October 17th, 2025</p>
|
||||
<h1>Why I still use Perl</h1>
|
||||
<div>
|
||||
This website, my <a href="https://molluscsoftware.com">companies website</a>, and soon,
|
||||
<a href="https://github.com/perl-ide/perl-ide-poll">the Perl IDE survey</a> all run on the same
|
||||
Alpine Linux machine. My website and mollusc's website run as static-files served by <a href="https://caddyserver.com">Caddy</a>.
|
||||
In an attempt to simplify my life, I decided that the new Perl-IDE-Survey
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user