A brief introduction to Prima

Prima is a graphical toolkit actually developed and maintained by Dmitry Karasik, now at version 1.34. If offers a large set of feature that makes it a good choice for anyone looking for an event driven environment.

Prima basic concept is that of building an application as a process of creating windows, adding any needed menus, childs, images, properties, and relationships and then starting the whole.

The basic use directives to use Prima are:

use Prima;
use Prima::Application;
use Prima::ComboBox;

and so on or using the qw syntax:

use Prima qw (Application ComboBox);

When we have available the modules we could start creating a window that will be the main window of our application.

$mw = Prima::MainWindow->create (
        # here there are some options, mainly that more common:
        size => [400,400],
        menuItems => [[ "~File" => [
        [],# division line
        [ "E~xit" => "Exit"    ]
        # events
        onPaint => sub {
        my ($self, $canvas) = @_;
        $canvas-> clear;
        $canvas-> color(cl::Red);
        $canvas-> line( 0, 0, 800, 800);

After we could add an object to the created window $mw using the insert:

        name => 'ComboBox',
        size => [ 100, 100],

and after this work we run the:

run Prima;

to get all built and executed.

Options used in the main window for this conceptual example, are size and menuItems. About the size there no much to say at this time but I would already spend some word on menuItems.

As shown the menuItems gets the menu structure. In real examples, as I will show later, it is a better and suggested solution to separate the menu in a subroutine that returns the menu and change the menuItems to:

menuItems => menu(),

The snippet contains also an example of events. In this case we have the onPaint event, which is triggered when the windows is painted. When the event occurs the sub will be executed, at least at the beginning when the windows is created, causing the line to be drawn on the screen.


Wow, this is really neat and works and actually looks good on Windows 7! Thanks for the write-up.

I've combined your example code into a gist here: https://gist.github.com/3047819

You could make the post a bit easier to read by doing that in the future and using the javascript embed you can get for gists under "Embed All Files:" to show the code in your post. :)

Since people on reddit asked, here's a screenshot: https://dl.dropbox.com/u/10190786/prima_test.png

I feel compelled to mention that David Mertens has used Prima to build a set of tools for PDL that is still young but very exciting. Checkout PDL::Graphics::Prima and App::Prima::REPL.

Thanks for starting this thread of posts! I agree that getting from zero to Prima is a bit tricky. I started with the tutorial followed by looking through the examples. I look forward to your writeups because a comprehensive introduction would be a great help for beginners.

I'd also like to thank Fabio for his efforts! I myself lack the style to present material easy enough for the beginners, so if this series will continue that would be great

I only find out about Prima recently. Love it! Does any body know tutorial on layout. I read the PDF manual but can not figure out the layout positioning for buttons, listbox..etc

Any chance of AnyEvent::Impl::Prima or IO::Async::Loop::Prima from the author?

Leave a comment

About Fabio D'Alfonso

user-pic A blog about Perl and Prima. I would write about Perl and Prima the way I would like to read about them.