The Definitive Guide to 2D Graphics with Perl#
Perl is not the language people reach for when they think “graphics”, and that reputation is mostly an accident of history rather than a statement about the tools. The vector engine everyone else uses under the hood (Cairo) ships natively here, SDL2 gives a window and an accelerated renderer, and the raster libraries are mature. What Perl has lacked is not libraries but a single place that puts them in order, from the pixel up to a running program.
This guide is that place, for two dimensions. It starts at the theory every graphics programmer eventually needs (how a line becomes pixels, what a colour space actually is, how transparency composites), moves through the working CPAN toolkits, and then goes one step further than the usual tour: it uses pperl’s native FFI to reach C graphics libraries directly, the same way pperl already reaches libgmp to give you Math::GMP. It ends with two things you can run: a drawing application and a 2D game.
Three dimensions and the GPU are the subject of the companion 3D guide, which builds on this one. The final chapter draws the line between the two.
Which chapter do I want?#
I want to … | Start here |
|---|---|
understand how pixels, paths, and colour actually work | |
implement a line, circle, fill, or blur myself | |
draw on, load, or save a bitmap image | |
draw on, load, or save a bitmap with GD | |
use an ImageMagick codebase, or its specific features | |
produce resolution-independent vector paths, or SVG output | |
read image metadata, or draw a chart | |
draw in a live window on Linux | |
reach a C graphics library that has no CPAN binding | |
see how pperl’s shipped native | |
build a real drawing application | |
build a real 2D game, or drive SDL2 directly | |
go on to 3D and the GPU |
The three arcs of this guide#
Foundations - foundations and raster-algorithms. The mental model and the classic algorithms, written in plain Perl so you own them before any library hides them. Read these once; every later chapter assumes the vocabulary.
Toolkits - the six chapters from raster-imager through gui-toolkits. What each library is good at and where one wins over another, ending with the window and frame loop. Each chapter is self-contained; reach for the one that names your problem.
Reaching further - ffi-graphics-primer onward. pperl’s native FFI, the two capstone projects, and an honest map of where the road currently ends.
A note on the runtime#
These chapters target pperl, a faithful Rust reimplementation of Perl 5.44. The CPAN modules in the toolkit chapters are used exactly as they are on any Perl 5, and the FFI chapters use pperl’s own Peta::FFI, which has no perl5 equivalent.
Known gaps#
Everything three-dimensional, every GPU-programming topic, computer vision and image analysis, and connectors to hosted image-generation services are out of scope for this version. They are named, with the reason and the plan, in what’s next. The FFI chapters also mark, at the point where it matters, which C interfaces Peta::FFI reaches and what a hand-written binding costs you.
See also#
Auto-FFI (Peta::FFI) - the native FFI layer the “reaching further” arc is built on:
dlopen,call, the type-signature codes, and the native bindings built on it.PPerl Architecture guide - the JIT and how pperl provides C-backed modules natively.