Skip to content

Instantly share code, notes, and snippets.

@run4flat
Created June 28, 2012 13:37
Show Gist options
  • Save run4flat/3011431 to your computer and use it in GitHub Desktop.
Save run4flat/3011431 to your computer and use it in GitHub Desktop.
POE::Loop::Prima Hello World (counter)
#!/usr/bin/perl
# based on http://poe.perl.org/?POE_Cookbook/Gtk_Interfaces
#
# This sample program creates a very simple Prima counter. Its
# interface consists of two widgets: A counter label and a button
# to reset that counter.
use warnings;
use strict;
# Prima support is enabled if the Prima module is used before POE itself.
use Prima qw(Application Label Buttons);
use POE;
# Create the session that will drive the user interface.
POE::Session->create(
inline_states => {
_start => \&ui_start,
ev_count => \&ui_count,
ev_clear => \&ui_clear,
}
);
# Run the program until it is exited.
$poe_kernel->run();
exit 0;
# Create the user interface when the session starts. This assumes
# some familiarity with Prima. ui_start() illustrates four important
# points.
#
# 1. Prima events do not require require a main window. It is therefore
# the responsibility of the programmer to create a main window (or
# not) herself. POE::Kernel's signal_ui_destroy() method attaches a
# UIDESTROY signal to the destruction of a window. In this case,
# closing the main window signals the program to shut down.
#
# 2. Widgets we need to work with later, such as the counter display,
# must be stored somewhere. The heap is a convenient place for them.
#
# 3. Prima widgets expect callbacks in the form of coderefs. The
# session's postback() method provides coderefs that post events when
# called. The Button created in ui_start() fires an "ev_clear" event
# when it is pressed.
#
# 4. POE::Kernel methods such as yield(), post(), delay(), signal(),
# and select() (among others) work the same as they would without Prima.
# This feature makes it possible to write back end sessions that
# support multiple GUIs with a single code base.
sub ui_start {
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
$heap->{main_window} = Prima::MainWindow->new(
text => 'Prima IRC Client',
size => [100, 100],
);
$kernel->signal_ui_destroy($heap->{main_window});
$heap->{counter_label} = $heap->{main_window}->insert(Label =>
place => {
x => 0, rely => 0.5, relheight => 0.5, relwidth => 1, anchor => 'sw',
},
text => '0',
);
$heap->{main_window}->insert(Button =>
place => {
x => 0, rely => 0, relheight => 0.5, relwidth => 1, anchor => 'sw',
},
text => 'Reset',
onClick => $session->postback("ev_clear"),
);
$kernel->yield("ev_count");
}
# Handle the "ev_count" event by increasing a counter and displaying
# its new value.
sub ui_count {
my ($kernel, $heap) = @_[KERNEL, HEAP];
my $value = $heap->{counter_label}->text;
$heap->{counter_label}->text($value + 1);
$kernel->yield("ev_count");
}
# Handle the "ev_clear" event by clearing and redisplaying the
# counter.
sub ui_clear {
$_[HEAP]->{counter_label}->text(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment