Download PDF
This tutorial is for those familiar with Perl who want to explore graphical user interface (GUI) development using Perl Prima. If you already know Perl, this guide will help you leverage that knowledge to build interactive, user-friendly applications with Prima.
To start with Prima, you should be comfortable with Perl basics:
If you're new to Perl, review these basics first. Once you're comfortable with these concepts, you'll be ready to build interactive GUI applications! Visit my website for additional tips, examples, and resources: https://reiniermaliepaard.nl/perl/
This tutorial covers essential concepts, including:
By the end, you'll have the skills to build and customize your own Perl Prima applications with interactive and responsive interfaces.
You can use this tutorial in two ways:
On this website:
This tutorial is licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0). You are free to share and adapt this material for any purpose, as long as you give appropriate credit and distribute any derivative works under the same license. For more information, visit creativecommons.org/licenses/by-sa/4.0/
Perl Prima is a robust and versatile toolkit for building graphical user interfaces (GUIs) in Perl. Over the years, I've developed a variety of Windows applications - ranging from educational and math tools to music and data visualization software. Now that I've fully transitioned to Linux, I wanted to continue creating GUI tools, and Perl Prima has proven to be the ideal choice.
The official documentation, while comprehensive, can feel overwhelming for beginners because of its technical depth and specialized terminology. This tutorial aims to bridge that gap by presenting Prima in a clear, approachable way.
When I first started with Perl Prima, I was excited but quickly ran into challenges. The documentation, while thorough, often lacked a beginner-friendly approach with practical examples and clear explanations.
This inspired me to create a tutorial that breaks complex concepts into simple, actionable steps. My goal is to help you avoid the difficulties I encountered and make your learning journey smooth and enjoyable.
Prima works seamlessly across Linux, Windows, and UNIX/X11 (including FreeBSD, IRIX, SunOS, Solaris, and more).
Prima is a modern cross-platform GUI toolkit featuring a sleek, flat design that integrates well with contemporary desktop environments.
Prima includes a RAD-style Visual Builder, a WYSIWYG tool for designing and testing GUIs quickly and intuitively.
Prima is actively maintained by its original developer, Dmitry Karasik, ensuring stability and long-term reliability.
See Appendix B to learn how to install Perl Prima.
Before exploring Prima in detail, let's create a small working program so you can see how a graphical application behaves. Let's make a compact version of a Prima application: a window (200x200) with a title and a centered button with text 'Exit!' and closes the application when the button is clicked. All in a few lines of code!
#!/usr/bin/perl use strict; use warnings; use Prima qw(Application Buttons); Prima::MainWindow->new( text => 'Your first Prima script', size => [200, 200] )->insert(Button => centered => 1, text => 'Exit!', onClick => sub { $::application->close } ); Prima->run;
Don't worry if the code seems complicated. Everything will become clear later on.
How to run it?
To save space, the following essential code will be omitted from the examples but should be included at the start of every script -see the complete listing files for reference:
#!/usr/bin/perl use strict; use warnings;
Although I don’t always follow the naming conventions in this tutorial, readers are encouraged to do so.
See Appendix A: Naming Conventions for the complete set of rules.
Key points to keep in mind:
While conciseness is often celebrated in programming, clarity should never be sacrificed for the sake of brevity. Redundancy, in this context, is not a flaw - it’s a deliberate choice to make the code self-documenting and easy to understand at a glance. "Don’t make me think" is a guiding principle here. Code should be immediately comprehensible, without mental parsing of clever shortcuts.
So I like:
push (@checked_options, $chk_lisp->text) if $chk_lisp->checked; push (@checked_options, substr($chk_perl->text, 1)) if $chk_perl->checked; push (@checked_options, $chk_python->text) if $chk_python->checked;
above
my @checked_options = map { $_->checked ? ( $_ == $chk_perl ? substr($_->text, 1) : $_->text ) : () } ($chk_lisp, $chk_perl, $chk_python);
In the previous chapter we created our first Prima program. You may have noticed syntax such as
We’ll first introduce general OOP concepts, and in later chapters, we’ll show how they are implemented in Perl - the language on which Prima is built.
Prima is designed in an object-oriented style, which involves key concepts like classes, objects, properties, methods, and inheritance.
In short, a class defines what an object can do and contain, while an object is a real instance of that class.
This concept will be reflected in Perl using packages (classes) and bless (object creation). More on this in Section 5.5.
Objects are characterized by properties and methods:
In Perl, properties are usually stored in a hash reference, and methods are subroutines defined in the package representing the class.
In event-driven programming, applications respond to user or system actions (e.g., button clicks, mouse movements).
Prima, being a GUI framework, heavily uses events, so understanding this concept helps when connecting Perl objects to interface actions.
Inheritance allows a class (subclass) to reuse attributes and methods from another class (superclass).
These concepts - classes, objects, methods, properties, and inheritance - will now be translated into Perl’s OOP style, which is flexible and minimalistic.
Perl does not have a built-in
package Animal; package Dog;
my $data = {}; # regular hash reference
my $object = bless($data, 'Dog'); # $object is now a Dog object
$object->speak(); # calls Dog::speak if defined
bless does not alter the data; it simply tells Perl which package’s methods to use.
my $d = Dog->new();
new is a regular method, not a keyword.
sub speak { my $self = shift; print "Dog says: Woof!\n"; }
# Option 1: direct @ISA assignment our @ISA = ('Animal');# Option 2: use base use base 'Animal';
package Dog; use base 'Animal'; sub speak { my $self = shift; print "Dog says: Woof!\n"; }
# --- Base Class: Animal --- { package Animal; sub new { my $class = shift; my $self = {}; bless($self, $class); return $self; } sub speak { my $self = shift; print "Animal makes a sound\n"; } }# --- Subclass: Dog --- { package Dog; use base 'Animal'; sub speak { my $self = shift; print "Dog says: Woof!\n"; } }# --- Subclass: Cat --- { package Cat; use base 'Animal';# inherits speak }# --- Main Script --- { my $a = Animal->new(); my $d = Dog->new(); my $c = Cat->new(); $a->speak();# Animal makes a sound $d->speak();# Dog says: Woof! $c->speak();# Animal makes a sound }
To separate classes into files: place them in
Animal.pm ,Dog.pm ,Cat.pm and load them inmain.pl usinguse Animal; use Dog; use Cat; . Each.pm must end with1; .
package Cat; use base 'Animal'; sub new { my $class = shift; my $self = $class->SUPER::new(); # delegate to parent constructor $self->{color} = shift || 'black'; return $self; } my $c = Cat->new('white'); print "Color cat: " . $c->{color} . "\n";# Color cat: white
Using
SUPER::new() avoids duplicating initialization logic and ensures a consistent object structure.
GUI development in Perl Prima isn’t just writing code; it’s learning to put together windows, buttons, and event handlers so your program can interact with users. Take your time, try things out, and keep in mind that every application - no matter how refined - began with a simple first step.